Linked List Cycle leetcode II java (寻找链表环的入口)
来源:互联网 发布:淘宝客招代理话术 编辑:程序博客网 时间:2024/06/18 06:57
题目:
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Follow up:
Can you solve it without using extra space?
题解:
这个连同I都是很经典的题啦,刷CC150时候就折磨了半天。
其实就推几个递推公式就好。。首先看图(图引用自CC150):
从链表起始处到环入口长度为:a,从环入口到Faster和Slower相遇点长度为:x,整个环长为:c。
明确了以上信息,就可以开始做运算了。。
假设从开始到相遇,Slower走过的路程长为s,由于Faster的步速是Slower的2倍,那么Faster在这段时间走的路程长为2s。
而对于Faster来说,他走的路程还等于之前绕整个环跑的n圈的路程nc,加上最后这一次遇见Slower的路程s。
所以我们有:
2s = nc + s
对于Slower来说,他走的路程长度s还等于他从链表起始处到相遇点的距离,所以有:
s = a + x
通过以上两个式子代入化简有:
a + x = nc
a = nc - x
a = (n-1)c + c-x
a = kc + (c-x)
那么可以看出,c-x,就是从相遇点继续走回到环入口的距离。上面整个式子可以看出,如果此时有个pointer1从起始点出发并且同时还有个pointer2从相遇点出发继续往前走(都只迈一步),那么绕过k圈以后, pointer2会和pointer1在环入口相遇。这样,换入口就找到了。
Reference: http://www.cnblogs.com/springfor/p/3862125.html
代码如下:
public ListNode detectCycle(ListNode head) { if(head==null||head.next==null) return null; ListNode fast = head,slow=head; while (true) { if (fast == null || fast.next == null) { return null; } slow = slow.next; fast = fast.next.next; if(fast==slow) break; } slow = head;//slow back to start point while(slow != fast){ slow = slow.next; fast = fast.next; } return slow; //when slow == fast, it is where cycle begins }
- Linked List Cycle leetcode II java (寻找链表环的入口)
- Linked List Cycle II 链表环的入口
- Linked List Cycle II 找链表环入口 @LeetCode
- Linked List Cycle II 求单链表环的入口
- Leetcode Linked List Cycle II 循环链表入口
- LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)
- [Leetcode] Linked List Cycle II (Java)
- [LeetCode] 142. Linked List Cycle II java
- leetcode:142. Linked List Cycle II(Java)解答
- leetcode linked-list-cycle(i 、ii)(java实现)
- (leetcode)Linked List Cycle II
- 求单链表环的入口结点 Linked List Cycle II
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Refletion2017.8.14
- SQL——字符匹配LINK
- sql语句
- Python Qt GUI快速编程第六章代码分析
- 计算(a*b)%c
- Linked List Cycle leetcode II java (寻找链表环的入口)
- 简洁直观地区别break与continue
- DAY 5の刷题
- 剑指offer-12:打印1到最大的n位数
- js事件
- android IPC(进程间通信)机制
- JQuery
- [DP] BZOJ1055: [HAOI2008]玩具取名
- Common Subsequence