We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
来自leetcode讨论区
k
s
m
r
2k-k=nr=r
s=k-m=r-m
a
r-m
b
class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } public class Solution { public ListNode detectCycle(ListNode head) { ListNode p1 = head, p2 = head; while (p1 != null && p2 != null) { p1 = p1.next; if (p2.next == null) { return null; } p2 = p2.next.next; if (p1 == p2) { break; } } if (p1 == null || p2 == null) { return null; } p1 = head; while (p1 != p2) { p1 = p1.next; p2 = p2.next; } return p1; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
基本思路
证明
k
步相遇s
长度m
r
2k-k=nr=r
s=k-m=r-m
a
走了不到整条链,比整条链少r-m
长度,然后b
走了整条链加上m
长度b
比a
多走的可以分成两段a
也走过(所以这一部分b
刚好走过两倍a
走过的长度)a
从链的起点到环的起点r-m
步就可以相遇代码
The text was updated successfully, but these errors were encountered: