From 8d3681f0e31cfcb1dbc2e02fe44089c2d1aff8d0 Mon Sep 17 00:00:00 2001 From: jaredliw Date: Thu, 15 Jul 2021 11:24:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0py=E5=92=8Cjs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../链表篇/leetcode142环形链表2.md | 74 +++++++++++++++---- 1 file changed, 59 insertions(+), 15 deletions(-) diff --git a/animation-simulation/链表篇/leetcode142环形链表2.md b/animation-simulation/链表篇/leetcode142环形链表2.md index 3e6e452..a92a73f 100644 --- a/animation-simulation/链表篇/leetcode142环形链表2.md +++ b/animation-simulation/链表篇/leetcode142环形链表2.md @@ -197,21 +197,21 @@ public class Solution { public ListNode detectCycle(ListNode head) { //快慢指针 ListNode fast = head; - ListNode low = head; + ListNode slow = head; //设置循环条件 while (fast != null && fast.next != null) { fast = fast.next.next; - low = low.next; + slow = slow.next; //相遇 - if (fast == low) { + if (fast == slow) { //设置一个新的指针,从头节点出发,慢指针速度为1,所以可以使用慢指针从相遇点出发 - ListNode newnode = head; - while (newnode != low) { - low = low.next; - newnode = newnode.next; + ListNode newptr = head; + while (newptr != slow) { + slow = slow.next; + newptr = newptr.next; } //在环入口相遇 - return low; + return slow; } } return null; @@ -227,26 +227,70 @@ public: ListNode *detectCycle(ListNode *head) { //快慢指针 ListNode * fast = head; - ListNode * low = head; + ListNode * slow = head; //设置循环条件 while (fast != nullptr && fast->next != nullptr) { fast = fast->next->next; - low = low->next; + slow = slow->next; //相遇 - if (fast == low) { + if (fast == slow) { //设置一个新的指针,从头节点出发,慢指针速度为1,所以可以使用慢指针从相遇点出发 ListNode * newnode = head; - while (newnode != low) { - low = low->next; + while (newnode != slow) { + slow = slow->next; newnode = newnode->next; } //在环入口相遇 - return low; + return slow; } } return nullptr; - } }; ``` +JS Code: + +```js +var detectCycle = function(head) { + let fast = head; + let slow = head; + while (fast && fast.next) { + fast = fast.next.next; + slow = slow.next; + if (fast == slow) { + let newptr = head; + while (newptr != slow) { + slow = slow.next; + newptr = newptr.next; + } + return slow; + } + } + return null; +}; +``` + +Python Code: + +```py +class Solution: + def detectCycle(self, head: ListNode) -> ListNode: + # 快慢指针 + fast = head + slow = head + # 设置循环条件 + while fast is not None and fast.next is not None: + fast = fast.next.next + slow = slow.next + # 相遇 + if fast is slow: + # 设置一个新的指针,从头节点出发,慢指针速度为1,所以可以使用慢指针从相遇点出发 + newptr = head + while newptr is not slow: + slow = slow.next + newptr = newptr.next + # 在环入口相遇 + return slow +``` +