LeetCode_Linked List Cycle
2014-05-11 19:17
232 查看
Given a linked list, determine if it has a cycle in it.
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: bool hasCycle(ListNode *head) { if (head==NULL) { return false; } ListNode *pSingleSpeed=head; ListNode *pDpubleSpeed=head; while (pSingleSpeed!=NULL&&pDpubleSpeed!=NULL) { pSingleSpeed=pSingleSpeed->next; pDpubleSpeed=pDpubleSpeed->next; if (pDpubleSpeed!=NULL) { pDpubleSpeed=pDpubleSpeed->next; } else { return false; } if (pSingleSpeed==pDpubleSpeed&&pSingleSpeed!=NULL &&pDpubleSpeed!=NULL) { return true; } } return false; } };
相关文章推荐
- leetcode--Linked List Cycle--判断链表是否有环
- LeetCode[Linked List]: Linked List Cycle II
- LeetCode - Linked List Cycle II
- Leetcode-Linked List Cycle II
- [LeetCode] Linked List Cycle
- [Leetcode] Linked List Cycle II
- [LeetCode] Linked List Cycle II, Solution
- leetcode: (141) Linked List Cycle
- Leetcode78: Linked List Cycle II
- 【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】
- [LeetCode]Linked List Cycle
- (LeetCode 141/142)Linked List Cycle
- [LeetCode] Linked List Cycle
- 【Leetcode】Linked List Cycle II
- LeetCode141:Linked List Cycle
- LeetCode - Linked List Cycle II
- leetcode:142. Linked List Cycle II(Java)解答
- LeetCode 141 Linked List Cycle(链表判环)
- [LeetCode]93. Linked List Cycle II查找链表中环的起始节点
- 142 Linked List Cycle II [Leetcode]