Linked List Cycle II
2015-04-27 12:55
260 查看
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?
null.
Follow up:
Can you solve it without using extra space?
/** * 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, *slow = head; while(fast && fast->next){ fast = fast->next->next; slow = slow->next; if(fast == slow) break; } if(fast!=slow) return NULL; slow = head; while(slow!=fast){ slow = slow->next; fast = fast->next; } return slow; } };
相关文章推荐
- [LeetCode] Linked List Cycle II 链表环起始位置
- ★ Linked List Cycle II -- LeetCode
- 142. Linked List Cycle II
- Linked List Cycle II Linked List Cycle
- [LeetCode] Linked List Cycle II, Solution
- 链表环查找(Linked List Cycle II)
- 【LeetCode】142 - Linked List Cycle II
- LinkedList-142-Linked List Cycle II
- leetcode linked-list-cycle(i 、ii)(java实现)
- 2.2.12—单链表—Linked List Cycle II
- leetcode - Linked List Cycle II
- 142. Linked List Cycle II
- LeetCode 之 Linked List Cycle II
- leetcode:linked-list-cycle-ii
- Linked List Cycle II
- Linked List Cycle II -- leetcode
- Easy 141题 Linked List Cycle Medium 142题 Linked List Cycle II
- Linked List Cycle II
- LeetCode | Linked List Cycle I,II
- Linked List Cycle II