[leetcode 141] Linked List Cycle
2014-12-28 11:49
435 查看
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) { return false; } auto slow = head; auto fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) { return true; } } return false; } };
相关文章推荐
- 【LeetCode】141 - Linked List Cycle
- (LeetCode 141/142)Linked List Cycle
- LeetCode 141 — Linked List Cycle(C++ Java Python)
- LeetCode 141 Linked List Cycle
- Leetcode 141 Linked List Cycle
- leetcode141~Linked List Cycle
- LeetCode 141 Linked List Cycle
- LeetCode: Linked List Cycle [141]
- Leetcode[141]-Linked List Cycle
- LeetCode 141 Linked List Cycle(链表判环)
- [LeetCode]题解(python):141-Linked List Cycle
- 【leetcode】141/142Linked List Cycle(Floyd判圈算法)
- leetcode 141-Linked List Cycle
- 【leetcode】【141】Linked List Cycle
- 【LEETCODE】141-Linked List Cycle
- Leetcode 141 Linked List Cycle
- Leetcode 141 Linked List Cycle
- 【leetcode】【单链表】【141】Linked List Cycle
- [Leetcode]#141 Linked List Cycle
- LeetCode - 141/142 - Linked List Cycle