From 07c3db6288cd3d423cfe32a7690ecd07179de254 Mon Sep 17 00:00:00 2001 From: h2linlin Date: Thu, 13 May 2021 20:21:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200142.=E7=8E=AF=E5=BD=A2?= =?UTF-8?q?=E9=93=BE=E8=A1=A8II.md=20Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0142.环形链表II.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index 9622affc..cd6347ef 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -186,7 +186,35 @@ public: Java: +```java +public class Solution { + public ListNode detectCycle(ListNode head) { + // 1.寻找相遇点 + ListNode fast = head; + ListNode slow = head; + while (fast != null && fast.next != null) { + fast = fast.next.next; + slow = slow.next; + if (fast != slow) { + continue; + } + ListNode meet = fast; + + // 2.寻找入口点 + slow = head; + while (slow != fast) { + slow = slow.next; + fast = fast.next; + } + + return fast; + } + + return null; + } +} +``` Python: