leetcode---linked-list-cycle-ii---链表
2017-10-20 18:37
405 查看
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.
Follow up:
Can you solve it without using extra space?
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) return head; ListNode *fast = head; ListNode *slow = head; while(fast->next && fast->next->next) { fast = fast->next->next; slow = slow->next; if(fast->val == slow->val) { slow = head; while(fast && slow && fast->val != slow->val) { fast = fast->next; slow = slow->next; } return slow; } } return NULL; } };
相关文章推荐
- LeetCode Linked List Cycle II 计算带环的链表的起点
- Leetcode 141/142 (链表题) Linked List Cycle && 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 & Linked List Cycle II——单链表中的环
- Linked List Cycle leetcode II java (寻找链表环的入口)
- 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 & Linked List Cycle II——单链表中的环
- [LeetCode]93. Linked List Cycle II查找链表中环的起始节点
- 【题解】【链表】【Leetcode】Linked List Cycle II
- [LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- 【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】
- 【leetcode】【单链表】【142】Linked List Cycle II
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
- 【leetcode】【单链表】【142】Linked List Cycle II