Leetcode: Linked List Cycle II
2013-12-18 23:49
357 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return
Follow up:
Can you solve it without using extra space?
对应方式1 - 快慢指针
/**
* 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) {
return NULL;
}
ListNode *slow = head;
ListNode *fast = head->next;
while (fast != NULL && fast->next != NULL && fast != slow) {
slow = slow->next;
fast = fast->next->next;
}
// No cycle
if (fast == NULL || fast->next == NULL) {
return NULL;
}
// Has cycle
slow = head;
fast = fast->next;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
链表转置貌似找不到环的起点。
======================第二次======================
/**
* 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;
ListNode *slow = head;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
ListNode *cur = head;
while (cur != slow) {
cur = cur->next;
slow = slow->next;
}
return cur;
}
}
return NULL;
}
};
null.
Follow up:
Can you solve it without using extra space?
对应方式1 - 快慢指针
/**
* 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) {
return NULL;
}
ListNode *slow = head;
ListNode *fast = head->next;
while (fast != NULL && fast->next != NULL && fast != slow) {
slow = slow->next;
fast = fast->next->next;
}
// No cycle
if (fast == NULL || fast->next == NULL) {
return NULL;
}
// Has cycle
slow = head;
fast = fast->next;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
链表转置貌似找不到环的起点。
======================第二次======================
/**
* 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;
ListNode *slow = head;
while (fast != NULL && fast->next != NULL) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
ListNode *cur = head;
while (cur != slow) {
cur = cur->next;
slow = slow->next;
}
return cur;
}
}
return NULL;
}
};
相关文章推荐
- 【leetcode】【单链表】【142】Linked List Cycle II
- *Linked List Cycle II - Leetcode
- LeetCode解题报告—— Linked List Cycle II & Reverse Words in a String & Fraction to Recurring Decimal
- Leetcode: Linked List Cycle II
- [Leetcode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- [leetcode] Linked List Cycle II
- LeetCode:Linked List Cycle && Linked List Cycle II
- LeetCode Linked List Cycle II
- [leetcode] Linked List Cycle II
- LeetCode——linked-list-cycle-ii
- [LeetCode] Linked List Cycle II
- leetcode——142——Linked List Cycle II
- LeetCode | Linked List Cycle II
- 【LeetCode】Linked List Cycle II
- [leetcode] Linked List Cycle II
- [LeetCode] Linked List CycleII
- [LeetCode 第11题] -- Linked List Cycle II
- leetcode-Linked List Cycle II
- [LeetCode] - Linked List Cycle II