代码随想录Day4
1.两两交换链表中的节点
题目
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
![image-20230112221609724](https://i-blog.csdnimg.cn/blog_migrate/d87fe703a882823aa0918f90d7af81f9.png)
示例 1:
输入:head = [1,2,3,4]
输出:[2,1,4,3]
示例 2:
输入:head = []
输出:[]
示例 3:
输入:head = [1]
输出:[1]
提示:
-
链表中节点的数目在范围 [0, 100] 内
-
0 <= Node.val <= 100
思路
- 采用虚拟头节点的方法,交换链表的物理节点,而非其数值;遍历链表,改变链表指针指向
![image-20230114230712927](https://i-blog.csdnimg.cn/blog_migrate/31b866d4de5d6b155a20e851d6815071.png)
代码
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0); //虚拟头节点
ListNode* cur;
ListNode* temp; //临时节点
ListNode* temp1;
dummyHead -> next = head;
cur = dummyHead;
while(cur -> next != nullptr && cur -> next -> next != nullptr) {
temp = cur -> next; //存储节点1
temp1 = cur -> next -> next -> next; //存储节点3
cur -> next = cur -> next -> next; //步骤1
cur -> next -> next = temp; //步骤2
temp -> next = temp1; //步骤3
cur = cur -> next -> next; //更新cur指针
}
return dummyHead -> next; //返回头节点
}
};
小结
- cur指针交换前指向两交换节点的前一个节点;
- 终止条件是判断 cur -> next 和 cur -> next -> next 是否为空,且需先判断第一个条件,否则若判断第二个条件,可能会出现空指针异常;
- 注意步骤处理顺序;
- 时间复杂度:O(n);空间复杂度:O(1);
2.删除链表的倒数第N个节点
题目
19. 删除链表的倒数第 N 个结点 - 力扣(LeetCode)
给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
![image-20230112221521272](https://i-blog.csdnimg.cn/blog_migrate/5c4bc96a81bbb6c743fc6c7383188de6.png)
示例 1:
输入:head = [1,2,3,4,5], n = 2
输出:[1,2,3,5]
示例 2:
输入:head = [1], n = 1
输出:[]
示例 3:
输入:head = [1,2], n = 1
输出:[1]
提示:
-
链表中结点的数目为 sz
-
1 <= sz <= 30
-
0 <= Node.val <= 100
-
1 <= n <= sz
思路
- 快慢指针,使用虚拟头节点,快指针先移动 n+1 步,然后慢指针与快指针同时移动,直到快指针指向空,此时慢指针指向倒数第n个节点的前一个节点;
代码
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
ListNode* dummyHead = new ListNode(0); //虚拟头结点
ListNode* fast = dummyHead;;
ListNode* slow = dummyHead;;
dummyHead -> next = head;
ListNode* tmp; //临时节点
n++;
while(n-- && fast != nullptr) {
fast = fast -> next;
}
while(fast != nullptr) {
fast = fast -> next;
slow = slow -> next;
}
tmp = slow -> next;
slow -> next = slow -> next -> next;
delete tmp;
return dummyHead -> next;
}
};
小结
- 链表题一般都使用虚拟头节点规范方式;
3.链表相交
题目
面试题 02.07. 链表相交 - 力扣(LeetCode)
给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点,返回 null 。
图示两个链表在节点 c1 开始相交:
![image-20230112221852193](https://i-blog.csdnimg.cn/blog_migrate/83360dd4340433a0c0ab9a26b2fcffe5.png)
题目数据 保证 整个链式结构中不存在环。
注意,函数返回结果后,链表必须 保持其原始结构 。
示例 1:
![image-20230112221922842](https://i-blog.csdnimg.cn/blog_migrate/2fa9baf007df2cb02929afc49d318b23.png)
输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Intersected at '8'
解释:相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。
在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
示例 2:
![image-20230112221939849](https://i-blog.csdnimg.cn/blog_migrate/8dcd76468decf159eecef3bfb12508df.png)
输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Intersected at '2'
解释:相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。
从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。
在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
示例 3:
![image-20230112221954489](https://i-blog.csdnimg.cn/blog_migrate/7cac0e498f15834656970d6324fd0d17.png)
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。
由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
这两个链表不相交,因此返回 null 。
提示:
-
listA 中节点数目为 m
-
listB 中节点数目为 n
-
0 <= m, n <= 3 * 104
-
1 <= Node.val <= 105
-
0 <= skipA <= m
-
0 <= skipB <= n
-
如果 listA 和 listB 没有交点,intersectVal 为 0
-
如果 listA 和 listB 有交点,intersectVal == listA[skipA + 1] == listB[skipB + 1]
思路
- 双指针,首先测出两链表的长度,然后curA(指向较长链表)先移动两链表长度之差,两指针对齐,同时移动进行比较,如果相同则返回值,无相同值则返回空;
![image-20230115172437418](https://i-blog.csdnimg.cn/blog_migrate/e941dbf333c28cbea446ea7946ba2322.png)
代码
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
ListNode* curA = headA;
ListNode* curB = headB;
int lenA = 0, lenB = 0;
//求链表长度
while(curA != nullptr) {
lenA++;
curA = curA -> next;
}
while(curB != nullptr) {
lenB++;
curB = curB -> next;
}
//指向链表头节点
curA = headA;
curB = headB;
//保证curA指向最长链表,lenA为最长链表的长度
if(lenB > lenA) {
swap(lenA, lenB);
swap(curA, curB);
}
//求长度差
int gap = lenA - lenB;
//移动curA指针,使两指针位置齐平
while(gap--) {
curA = curA -> next;
}
//遍历两链表,比较交点
while(curA != nullptr && curB != nullptr) {
if(curA == curB) {
return curA;
}
curA = curA -> next;
curB = curB -> next;
}
return nullptr;
}
};
小结
- 时间复杂度:O(n+m);空间复杂度:O(1);
- 注意初始化问题,链表长度不初始化会报错;
- 注意最后遍历链表时,比较的是两指针是否指向同一节点,而不是两指针所指向值是否相同;
- 返回时,返回节点或空,不能返回节点所指向的值;
4.环形链表||
题目
Loading Question… - 力扣(LeetCode)
给定一个链表的头节点 head ,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
如果链表中有某个节点,可以通过连续跟踪 next 指针再次到达,则链表中存在环。 为了表示给定链表中的环,评测系统内部使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。如果 pos 是 -1,则在该链表中没有环。注意:pos 不作为参数进行传递,仅仅是为了标识链表的实际情况。
不允许修改 链表。
示例 1:
![image-20230112224134185](https://i-blog.csdnimg.cn/blog_migrate/d4fec29a86633490b56e468816e79c33.png)
输入:head = [3,2,0,-4], pos = 1
输出:返回索引为 1 的链表节点
解释:链表中有一个环,其尾部连接到第二个节点。
示例 2:
![image-20230112224112679](https://i-blog.csdnimg.cn/blog_migrate/25020f1690f9d995cc545f83460a62b5.png)
输入:head = [1,2], pos = 0
输出:返回索引为 0 的链表节点
解释:链表中有一个环,其尾部连接到第一个节点。
示例 3:
![image-20230112224038602](https://i-blog.csdnimg.cn/blog_migrate/8939053e9cfaddeca5aa9e0da0be0712.png)
输入:head = [1], pos = -1
输出:返回 null
解释:链表中没有环。
提示:
-
链表中节点的数目范围在范围 [0, 104] 内
-
-105 <= Node.val <= 105
-
pos 的值为 -1 或者链表中的一个有效索引
思路
- 快慢指针,快指针每次走两个节点,慢指针每次走一个节点,如果有环,则两指针一定会在链表里相遇,如果无环,则不会相遇;
![](https://i-blog.csdnimg.cn/blog_migrate/e2d1d21f602df78071ceca98b58f3bb4.gif)
- 难点:找到环的入口;
![](https://i-blog.csdnimg.cn/blog_migrate/c3e94f509dc4068992c331fac0b89493.png)
![image-20230115180811420](https://i-blog.csdnimg.cn/blog_migrate/4f38bbdfe90b4299983dea9a5f91012d.png)
代码
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode* fast = head; //快指针
ListNode* slow = head; //慢指针
while(fast != nullptr && fast->next != nullptr) {
fast = fast->next->next; //快指针每次走两步
slow = slow->next;; //慢指针每次走一步
if(fast == slow) { //快慢指针相遇
ListNode* index1 = fast;
ListNode* index2 = head;
while(index1 != index2) {
index1 = index1->next;
index2 = index2->next;
}
return index1;
}
}
return nullptr;
}
};
小结
- 由于快指针一次走两个节点,所以终止条件不仅要判断 fast 是否为空,还要判断 fast -> next 是否为空;
- 注意算法题其中的规律;
参考