先看这张图:
设:链表头是X,环的第一个节点是Y,快慢指针slow和fast,分别每次走1和2步,第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。
-
环的长度是多少?
-
如何找到环中第一个节点(即Linked List Cycle II)?
-
如何将有环的链表变成单链表(解除环)?
-
如何判断两个单链表是否有交点?如何找到第一个相交的节点?
1.方法一:
第一次相遇后,让slow,fast继续走,记录到下次相遇时循环了几次。因为当fast第二次到达Z点时,fast走了一圈,slow走了半圈,而当fast第三次到达Z点时,fast走了两圈,slow走了一圈,正好还在Z点相遇。
方法二:
第一次相遇后,让fast停着不走了,slow继续走,记录到下次相遇时循环了几次。
方法三:
第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。
因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!)。
我们发现L=b+c=a+b,也就是说,从一开始到二者第一次相遇,循环的次数就等于环的长度。
2.我们已经得到了结论a=c,那么让两个指针分别从X和Z开始走,每次走一步,那么正好会在Y相遇!也就是环的第一个节点。
3.在上一个问题的最后,将c段中Y点之前的那个节点与Y的链接切断即可。
4.如何判断两个单链表是否有交点?先判断两个链表是否有环,如果一个有环一个没环,肯定不相交;如果两个都没有环,判断两个列表的尾部是否相等;如果两个都有环,判断一个链表上的Z点是否在另一个链表上。
如何找到第一个相交的节点?求出两个链表的长度L1,L2(如果有环,则将Y点当做尾节点来算),假设L1<L2,用两个指针分别从两个链表的头部开始走,长度为L2的链表先走(L2-L1)步,然后两个一起走,直到二者相遇。
LeetCode: Linked List Cycle
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
题意:判断链表是否有环
解法一:使用unordered_map记录当前节点是否被访问过,如访问过返回该节点,如到达尾部说明无环。空间复杂度O(n),时间复杂度O(n)。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
unordered_map<ListNode*, bool>visited;
while(head != NULL){
if(visited[head] == true){
return true;
}
visited[head] = true;
head = head->next;
}
return false;
}
};
解法二:利用快慢指针,快指针一次走两步,慢指针一次一步,若有环两个指针一定会相遇。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head, *fast = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast) return true;
}
return false;
}
};
LeetCode: Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
题意:找出带环链表环的起点。
解法一:还是用unordered_map,第一个重复的结点就是那个起点。
/**
* 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) {
unordered_map<ListNode*, bool> visited;
while(head != NULL){
if(visited[head] == true){
return head;
}
visited[head] = true;
head = head->next;
}
return NULL;
}
};
解法二:即分析的结果:已经得到了结论a=c,那么让两个指针分别从X和Z开始走,每次走一步,那么正好会在Y相遇!也就是环的第一个节点。
/**
* 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) {
ListNode *fast = head, *slow = head, *slow2 = head;
while(fast && fast->next){
slow = slow->next;
fast = fast->next->next;
if(slow == fast){
while(slow2 != slow){
slow2 = slow2->next;
slow = slow->next;
}
return slow;
}
}
return NULL;
}
};