(leetcode)Linked List Cycle
2014-08-19 21:54
260 查看
题目描述:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题目分析:判断一个离岸边是否有环路。这个是经常在经典算法题上看到的一个题,主要是记着了其题解:两个指针从头指针开始向后移动,其中一个快指针(每次后移动两个节点),一个慢指针(每次移动一个节点),若这两个指针可以相遇,那就有环路,不能相遇,就没有环路。
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
题目分析:判断一个离岸边是否有环路。这个是经常在经典算法题上看到的一个题,主要是记着了其题解:两个指针从头指针开始向后移动,其中一个快指针(每次后移动两个节点),一个慢指针(每次移动一个节点),若这两个指针可以相遇,那就有环路,不能相遇,就没有环路。
public boolean hasCycle(ListNode head) { if(head==null||head.next==null) return false; ListNode next2=head; while(head!=null&&next2!=null&&next2.next!=null){ head=head.next; next2=next2.next.next; if(head==next2){ return true; } } return false; }
相关文章推荐
- LeetCode 141 Linked List Cycle(链表判环)
- LeetCode: Linked List Cycle II [142]
- [LeetCode]Linked List Cycle II
- leetcode_c++:链表:Linked List Cycle(141)
- leetcode 141-Linked List Cycle
- LeetCode:142. Linked List Cycle II
- Leetcode 141 Linked List Cycle
- [Leetcode][python]Linked List Cycle/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
- C实现 LeetCode->Linked List Cycle 双指针大法)(单链表是否有环)
- LeetCode-141 Linked List Cycle
- LeetCode:Linked List Cycle && Linked List Cycle II
- Leetcode: Linked List Cycle
- LeetCode -- LinkedListCycle
- LeetCode 解题报告 LinkedListCycleII
- leetcode--Linked List Cycle II