【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II
LeetCode24. 两两交换链表中的节点
题目链接:24. 两两交换链表中的节点
初次尝试
比较暴力的解法,利用三个指针,进行类似反转链表里面的反转next指针指向的操作,然后三个指针整体向后移动到下一组节点,暴力但是ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == NULL || head -> next == NULL) return head;
ListNode* pre = head;
ListNode* cur = head -> next;
ListNode* temp = cur -> next;
head = head -> next;
while (true) {
cur -> next = pre;
pre -> next = temp;
if (temp != NULL && temp -> next != NULL) {
cur = temp -> next;
pre -> next = cur;
pre = temp;
temp = cur -> next;
}
else break;
}
return head;
}
};
看完代码随想录后的想法
思路差不多,忘记用虚拟头结点了,重新用虚拟头结点写了一下,ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
if (head == NULL || head -> next == NULL) return head;
ListNode* dummyHead = new ListNode(0, head);
ListNode* cur = dummyHead;
while (cur -> next != NULL && cur -> next -> next != NULL) {
ListNode* temp1 = cur -> next;
ListNode* temp2 = cur -> next -> next;
cur -> next = temp2;
temp1 -> next = temp2 -> next;
temp2 -> next = temp1;
cur = cur -> next -> next;
}
return dummyHead -> next;
}
};
LeetCode19. 删除链表的倒数第N个结点
题目链接:19. 删除链表的倒数第N个结点
初次尝试
暴力解法,先遍历链表得出链表的长度,然后计算出倒数第n个结点的索引,然后删除,ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* newHead = new ListNode(0, head);
ListNode* p = head;
int listSize = 0;
while (p != NULL) {
p = p -> next;
listSize++;
}
p = newHead;
ListNode* temp;
int delIndex = listSize - n;
while (delIndex > 0) {
p = p -> next;
delIndex--;
}
temp = p -> next;
p -> next = temp -> next;
delete temp;
return newHead -> next;
}
};
看完代码随想录后的想法
题解利用了快慢指针,快指针先移动n个结点,然后快慢指针同时移动,在移动过程中保持n距离不变,这样快指针到达链表尾部的时候,慢指针就正好位于要删除的结点,十分巧妙的思路,重新写一遍ac。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0, head);
ListNode* fast = dummyHead;
ListNode* slow = dummyHead;
for (; n > 0; n--) {
fast = fast -> next;
}
while (fast -> next != NULL) {
fast = fast -> next;
slow = slow -> next;
}
ListNode* temp = slow -> next;
slow -> next = temp -> next;
delete temp;
return dummyHead -> next;
}
};
LeetCode面试题 02.07. 链表相交
题目链接:面试题 02.07. 链表相交
初次尝试
没有想到解法,思路卡在了单链表不能回溯上面。
看完代码随想录后的想法
题解考虑到了两个链表的长度差,感觉和今天的第二题有异曲同工之妙,虽然单链表不能回溯,但是如果能提前知道需要回溯的长度,在第二题中就是倒数第n个,在本题中就是两个链表长度的差,这就是这类题中核心的“不变量”,通过使用保持这个距离同时移动的快慢指针,就可以从单链表不能回溯的思维定势中走出来。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* pA = headA;
ListNode* pB = headB;
int aSize = 0, bSize = 0;
while (pA != NULL) {
aSize++;
pA = pA -> next;
}
while (pB != NULL) {
bSize++;
pB = pB -> next;
}
int offset = aSize - bSize;
pA = headA;
pB = headB;
if (offset >= 0) {
for (; offset != 0; offset--) {
pA = pA -> next;
}
}
else {
for (; offset != 0; offset++) {
pB = pB -> next;
}
}
while (pA != NULL) {
if (pA == pB) return pA;
pA = pA -> next;
pB = pB -> next;
}
return NULL;
}
};
LeetCode142. 环形链表II
题目链接:142. 环形链表II
初次尝试
没有想到解法,两年前见过用快慢指针解环链表问题的解法,解本题的时候也一直想怎么用来着?最后还是没有想起来具体是怎么用的。
看完代码随想录后的想法
刚看到需要数学计算的提示,就开始想自己先算一算,一开始以为是可以通过数学计算直接解出入环结点的索引,所以一直致力于解出具体的数字,后来发现解不出来就放弃了,完整看了视频题解,解法真的很巧妙,并没有解出什么具体的数字,而是通过理解等式本身的意义,得出了获得入环结点索引的方法,虽然这个解法不具有大规模的应用能力,但是很锻炼思维能力和计算能力。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if (head == NULL || head -> next == NULL) return NULL;
ListNode* fast = head;
ListNode* slow = head;
while (fast != NULL && fast -> next != NULL) {
fast = fast -> next -> next;
slow = slow -> next;
if (fast == slow) {
ListNode* index1 = head;
ListNode* index2 = fast;
while (index1 != index2) {
index1 = index1 -> next;
index2 = index2 -> next;
}
return index1;
}
}
return NULL;
}
};
【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II的更多相关文章
- LeetCode 面试题 02.07. 链表相交
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists-lcci/ 给定两个(单向)链表,判定它们是否相交并返回交 ...
- leetcode面试题 02.06. 回文链表,解题心路
目录 leetcode面试题 02.06. 回文链表,解题心路 1.题目描述 2.java语言题解一 3.java语言题解二 4.C语言题解一 leetcode面试题 02.06. 回文链表,解题心路 ...
- SDUT OJ 数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Problem ...
- SDUT-2122_数据结构实验之链表七:单链表中重复元素的删除
数据结构实验之链表七:单链表中重复元素的删除 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 按照数据输入的相反顺序(逆 ...
- LeetCode142 环形链表 II
给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 说明:不允许修改给定的链表. 进阶:你是否可以不用额外空间解决此题? //章节 - 链表 //二.双指针技巧 //2.环 ...
- 【leetcode 简单】 第三十五题 环形链表
给定一个链表,判断链表中是否有环. 进阶: 你能否不使用额外空间解决此题? /** * Definition for singly-linked list. * struct ListNode { * ...
- Leetcode142 环形链表
很多题解没有讲清楚非环部分的长度与相遇点到环起点那部分环之间为何是相等的这个数学关系.这里我就补充下为何他们是相等的.假设非环部分的长度是x,从环起点到相遇点的长度是y.环的长度是c.现在走的慢的那个 ...
- LeetCode 面试题 02.06. 回文链表
题目链接:https://leetcode-cn.com/problems/palindrome-linked-list-lcci/ 编写一个函数,检查输入的链表是否是回文的. 示例 1: 输入: 1 ...
- [Swift]LeetCode142. 环形链表 II | Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
随机推荐
- 只要9.9元!零基础学习MySQL
GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 导语 经过一段时间的筹备和整理,万里数据库<零基础学习MySQL>课程正式在腾讯课堂上线了. 课程地址:htt ...
- Sphere类定义
这个类是球体,也就是一会要显示的球体了.这个类继承于Geometrics类,并实现了自己的碰撞检测,碰撞原理,书上也说的很清楚了啊,大家多看.然后对照代码就明白了. 类定义: #pragma once ...
- POJ2201 Cartesian Tree (cartesian tree)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- 【建议收藏】Mac VMWare NAT模式安装 CentOS 7-操作教程
学习大数据离不开 Linux 系统,网络上大部分文章都是在 Windows 系统下使用 VMWare Workstation 安装 CentOS ,并使用 NAT 模式配置网络.本文基于 Mac OS ...
- Java Stream 函数式接口外部实例的引用
Java Function Interface 函数式接口: Stream.empty() .filter(Predicate) .map(Function) .forEach(Consumer); ...
- String vs StringBuffer vs StringBuilder
String vs StringBuffer vs StringBuilder 本文翻译自:https://www.digitalocean.com/community/tutorials/strin ...
- SpringBoot中maven项目Plugins里resources报红
错误原因:刚开始下载依赖时下载错误导致的 参考:解决idea中maven plugins标红的问题 - 走看看 (zoukankan.com) 如果还不行: 就根据上面提示的地址找到maven的配置包 ...
- Linux网桥配置(用于大数据虚拟化)
理解 VMware里面有三个虚拟机,分别为RHEL8,RHEL7,Windows的虚拟机,只有一个物理网卡连接物理网络,现在三台虚拟机都需要直连到物理网络,此时无法访问物理网络,只能给一个虚拟机访问物 ...
- zabbix_agentd断断续续端无法访问问题记录
问题现象: zabbix监控上出现zabbix_agentd无法访问,但是实际上zabbix_agentd是存活状态 每隔一段时间就会出现这样的情况 问题原因 zabbix_agentd端任务较多,活 ...
- KingbaseES例程之拥有大量索引的表导入数据
概述 如何快速插入大量数据比如几千万上亿的带索引的数据表. 数据准备 准备一个拥有二十个索引的数据表. kingbase=# \d+ bigtab Table "kingbase.bigta ...