Linked List Cycle II Linked List Cycle
2014-09-02 13:57
239 查看
地址:https://oj.leetcode.com/problems/linked-list-cycle/
https://oj.leetcode.com/problems/linked-list-cycle/
题意很简单第一题进行判断链表是否有环,第二个问题就是如果链表有环返回链表环的起始点。
判断链表是否有环很简单:在链表相关问题中经常用到这个方法,设定两个指针,一个快一个慢,如果两个指针相交则肯定存在环。
第二个问题判断如果链表有环,返回环的起始点。这里需要用到一点点数学推导:(通过简单示意图很明显可以推导)
当fast指针等于slow指针时,slow指针肯定还没有遍历完整个链表,而此时fast指针已经在环内循环了n圈(n>=1),假定从链表头指针开始slow走了s步,则fast走了2s步,fast所走的步数还等于s加上fast指针比slow指针在环内多走的n圈。设环长为r,则:
2s = s + nr;
=>s = nr;
设整个链表长度为L,环的入口结点到相遇结点的距离为x, 起点到环的入口结点的距离为a.
a + x = nr;
=> a + x = (n-1)r + L - a;
=> a = (n-1)r + (L - a - x);
=> 由链表的头结点到环入口结点的距离等于n-1圈环的长度+相遇点到环入口结点的距离,于是,当我们在链表头部和相遇处分别设一指针,每次各走一步,则两指针必定相遇,且相遇的第一个结点即为环的入口结点。实现比较简单:
https://oj.leetcode.com/problems/linked-list-cycle/
题意很简单第一题进行判断链表是否有环,第二个问题就是如果链表有环返回链表环的起始点。
判断链表是否有环很简单:在链表相关问题中经常用到这个方法,设定两个指针,一个快一个慢,如果两个指针相交则肯定存在环。
public class Solution { public boolean hasCycle(ListNode head) { if(head == null || head.next ==null){ return false; } ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null){ fast = fast.next.next; slow = slow.next; if(fast == slow){ return true; } } return false; } }
第二个问题判断如果链表有环,返回环的起始点。这里需要用到一点点数学推导:(通过简单示意图很明显可以推导)
当fast指针等于slow指针时,slow指针肯定还没有遍历完整个链表,而此时fast指针已经在环内循环了n圈(n>=1),假定从链表头指针开始slow走了s步,则fast走了2s步,fast所走的步数还等于s加上fast指针比slow指针在环内多走的n圈。设环长为r,则:
2s = s + nr;
=>s = nr;
设整个链表长度为L,环的入口结点到相遇结点的距离为x, 起点到环的入口结点的距离为a.
a + x = nr;
=> a + x = (n-1)r + L - a;
=> a = (n-1)r + (L - a - x);
=> 由链表的头结点到环入口结点的距离等于n-1圈环的长度+相遇点到环入口结点的距离,于是,当我们在链表头部和相遇处分别设一指针,每次各走一步,则两指针必定相遇,且相遇的第一个结点即为环的入口结点。实现比较简单:
public class Solution { public ListNode detectCycle(ListNode head) { if(head == null || head.next ==null){ return null; } ListNode fast = head; ListNode slow = head; while(fast!=null && fast.next!=null){ fast = fast.next.next; slow = slow.next; if(fast == slow){ break; } } if(fast == null || fast.next == null){ return null; } slow = head; while(slow != fast){ slow = slow.next; fast = fast.next; } return slow; } }
相关文章推荐
- Linked List Cycle II(LeetCode)
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
- [LeetCode]Linked List Cycle II
- Linked List Cycle II
- [Leetcode] Linked List Cycle II
- [leetcode] Linked List Cycle II
- 142. Linked List Cycle II
- LeetCode 142: Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II 找链表环入口 @LeetCode
- [Leetcode] Linked List Cycle II
- Linked List Cycle II--找出单向链表中环的起点
- LinkedList-142-Linked List Cycle II
- 2.2.12—单链表—Linked List Cycle II
- Linked List Cycle II
- LeetCode--Linked List Cycle II
- LeetCode Linked List Cycle II
- LeetCode 之 Linked List Cycle II
- LeetCode142:Linked List Cycle II
- Linked List Cycle II