Linked List Cycle II
2016-10-03 16:27
330 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, return
Note: Do not modify the linked list.
null.
Note: Do not modify the linked list.
/** * 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* slow = head; ListNode* fast = head; while(fast && fast->next){ slow = slow->next; fast = fast->next->next; if(slow == fast){ ListNode* slow2 = head; while(slow2 != slow){ slow = slow->next; slow2 = slow2->next; } return slow; } } return nullptr; } };
相关文章推荐
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- LeetCode: Linked List Cycle II
- Two pointers (1) -- Linked List Cycle II, Rotate List
- Linked List Cycle II
- 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][JavaScript]Linked List Cycle II
- Linked List Cycle II
- leecode 解题总结:142. Linked List Cycle II
- LeetCode | Linked List Cycle II
- 【leetcode】 Linked_List_Cycle_II
- 【LeetCode】Linked List Cycle II
- 又是一道快慢指针与链表的结合题 Linked List Cycle II
- [LeetCode]Linked List Cycle II
- LeetCode 141 Linked List Cycle和142 Linked List Cycle II
- LeetCode:Linked List Cycle && Linked List Cycle II