From a87c930e8932935541c1786ef26b8f8e48371535 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 3 Apr 2024 08:39:16 +0800 Subject: [PATCH 1/2] feat: add solutions to lcci/lcof2 problems MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * lcci No.02.08.Linked List Cycle * lcof2 No.022.链表中环的入口节点 * lcof2 No.023.两个链表的第一个重合节点 --- lcci/02.08.Linked List Cycle/README.md | 177 +++++++++++------- lcci/02.08.Linked List Cycle/README_EN.md | 173 ++++++++++------- lcci/02.08.Linked List Cycle/Solution.cpp | 24 ++- lcci/02.08.Linked List Cycle/Solution.go | 26 +-- lcci/02.08.Linked List Cycle/Solution.java | 24 ++- lcci/02.08.Linked List Cycle/Solution.js | 25 ++- lcci/02.08.Linked List Cycle/Solution.py | 23 ++- lcci/02.08.Linked List Cycle/Solution.ts | 28 +++ .../README.md" | 177 +++++++++++------- .../Solution.cpp" | 24 ++- .../Solution.go" | 26 +-- .../Solution.java" | 24 ++- .../Solution.js" | 25 ++- .../Solution.py" | 23 ++- .../Solution.ts" | 28 +++ .../README.md" | 109 +++++++---- .../Solution.cpp" | 11 +- .../Solution.cs" | 18 ++ .../Solution.go" | 18 +- .../Solution.java" | 14 +- .../Solution.js" | 12 +- .../Solution.py" | 10 +- .../Solution.ts" | 12 +- 23 files changed, 618 insertions(+), 413 deletions(-) create mode 100644 lcci/02.08.Linked List Cycle/Solution.ts create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.ts" create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cs" diff --git a/lcci/02.08.Linked List Cycle/README.md b/lcci/02.08.Linked List Cycle/README.md index ee36c2db7d841..f4d7812b41b06 100644 --- a/lcci/02.08.Linked List Cycle/README.md +++ b/lcci/02.08.Linked List Cycle/README.md @@ -10,7 +10,25 @@ ## 解法 -### 方法一 +### 方法一:快慢指针 + +我们先利用快慢指针判断链表是否有环,如果有环的话,快慢指针一定会相遇,且相遇的节点一定在环中。 + +如果没有环,快指针会先到达链表尾部,直接返回 `null` 即可。 + +如果有环,我们再定义一个答案指针 $ans$ 指向链表头部,然后让 $ans$ 和慢指针一起向前走,每次走一步,直到 $ans$ 和慢指针相遇,相遇的节点即为环的入口节点。 + +为什么这样能找到环的入口节点呢? + +我们不妨假设链表头节点到环入口的距离为 $x$,环入口到相遇节点的距离为 $y$,相遇节点到环入口的距离为 $z$,那么慢指针走过的距离为 $x + y$,快指针走过的距离为 $x + y + k \times (y + z)$,其中 $k$ 是快指针在环中绕了 $k$ 圈。 + +

+ +由于快指针速度是慢指针的 $2$ 倍,因此有 $2 \times (x + y) = x + y + k \times (y + z)$,可以推出 $x + y = k \times (y + z)$,即 $x = (k - 1) \times (y + z) + z$。 + +也即是说,如果我们定义一个答案指针 $ans$ 指向链表头部,然后 $ans$ 和慢指针一起向前走,那么它们一定会在环入口相遇。 + +时间复杂度 $O(n)$,其中 $n$ 是链表中节点的数目。空间复杂度 $O(1)$。 @@ -23,18 +41,17 @@ class Solution: - def detectCycle(self, head: ListNode) -> ListNode: - slow = fast = head - has_cycle = False - while not has_cycle and fast and fast.next: - slow, fast = slow.next, fast.next.next - has_cycle = slow == fast - if not has_cycle: - return None - p = head - while p != slow: - p, slow = p.next, slow.next - return p + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + fast = slow = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + if slow == fast: + ans = head + while ans != slow: + ans = ans.next + slow = slow.next + return ans ``` ```java @@ -51,22 +68,20 @@ class Solution: */ public class Solution { public ListNode detectCycle(ListNode head) { - ListNode slow = head, fast = head; - boolean hasCycle = false; - while (!hasCycle && fast != null && fast.next != null) { + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; + if (slow == fast) { + ListNode ans = head; + while (ans != slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - if (!hasCycle) { - return null; - } - ListNode p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; } } ``` @@ -83,23 +98,21 @@ public class Solution { class Solution { public: ListNode* detectCycle(ListNode* head) { - ListNode* slow = head; ListNode* fast = head; - bool hasCycle = false; - while (!hasCycle && fast && fast->next) { + ListNode* slow = head; + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return nullptr; + if (slow == fast) { + ListNode* ans = head; + while (ans != slow) { + ans = ans->next; + slow = slow->next; + } + return ans; + } } - ListNode* p = head; - while (p != slow) { - p = p->next; - slow = slow->next; - } - return p; + return nullptr; } }; ``` @@ -113,20 +126,51 @@ public: * } */ func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast - } - if !hasCycle { - return nil + fast, slow := head, head + for fast != nil && fast.Next != nil { + slow = slow.Next + fast = fast.Next.Next + if slow == fast { + ans := head + for ans != slow { + ans = ans.Next + slow = slow.Next + } + return ans + } } - p := head - for p != slow { - p, slow = p.Next, slow.Next - } - return p + return nil +} +``` + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function detectCycle(head: ListNode | null): ListNode | null { + let [slow, fast] = [head, head]; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } + } + return null; } ``` @@ -144,26 +188,19 @@ func detectCycle(head *ListNode) *ListNode { * @return {ListNode} */ var detectCycle = function (head) { - let slow = head; - let fast = head; - let hasCycle = false; - while (!hasCycle && fast && fast.next) { + let [slow, fast] = [head, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return null; - } - let p = head; - while (p != slow) { - p = p.next; - slow = slow.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - return p; + return null; }; ``` - - - - diff --git a/lcci/02.08.Linked List Cycle/README_EN.md b/lcci/02.08.Linked List Cycle/README_EN.md index b2d71ab97c1dd..eb7816651a316 100644 --- a/lcci/02.08.Linked List Cycle/README_EN.md +++ b/lcci/02.08.Linked List Cycle/README_EN.md @@ -38,7 +38,25 @@ Can you solve it without using additional space?

## Solutions -### Solution 1 +### Solution 1: Two Pointers + +We first use the fast and slow pointers to judge whether the linked list has a ring. If there is a ring, the fast and slow pointers will definitely meet, and the meeting node must be in the ring. + +If there is no ring, the fast pointer will reach the tail of the linked list first, and return `null` directly. + +If there is a ring, we then define an answer pointer $ans$ to point to the head of the linked list, and then let $ans$ and the slow pointer move forward together, moving one step at a time, until $ans$ and the slow pointer meet, and the meeting node is the ring entrance node. + +Why can this find the entrance node of the ring? + +Let's assume that the distance from the head node of the linked list to the entrance of the ring is $x$, the distance from the entrance of the ring to the meeting node is $y$, and the distance from the meeting node to the entrance of the ring is $z$. Then the distance traveled by the slow pointer is $x + y$, and the distance traveled by the fast pointer is $x + y + k \times (y + z)$, where $k$ is the number of times the fast pointer goes around the ring. + +

+ +Because the speed of the fast pointer is twice that of the slow pointer, it is $2 \times (x + y) = x + y + k \times (y + z)$, which can be deduced that $x + y = k \times (y + z)$, that is $x = (k - 1) \times (y + z) + z$. + +That is to say, if we define an answer pointer $ans$ to point to the head of the linked list, and the $ans$ and the slow pointer move forward together, they will definitely meet at the ring entrance. + +The time complexity is $O(n)$, where $n$ is the number of nodes in the linked list. The space complexity is $O(1)$. @@ -51,18 +69,17 @@ Can you solve it without using additional space?

class Solution: - def detectCycle(self, head: ListNode) -> ListNode: - slow = fast = head - has_cycle = False - while not has_cycle and fast and fast.next: - slow, fast = slow.next, fast.next.next - has_cycle = slow == fast - if not has_cycle: - return None - p = head - while p != slow: - p, slow = p.next, slow.next - return p + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + fast = slow = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + if slow == fast: + ans = head + while ans != slow: + ans = ans.next + slow = slow.next + return ans ``` ```java @@ -79,22 +96,20 @@ class Solution: */ public class Solution { public ListNode detectCycle(ListNode head) { - ListNode slow = head, fast = head; - boolean hasCycle = false; - while (!hasCycle && fast != null && fast.next != null) { + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return null; + if (slow == fast) { + ListNode ans = head; + while (ans != slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - ListNode p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; } } ``` @@ -111,23 +126,21 @@ public class Solution { class Solution { public: ListNode* detectCycle(ListNode* head) { - ListNode* slow = head; ListNode* fast = head; - bool hasCycle = false; - while (!hasCycle && fast && fast->next) { + ListNode* slow = head; + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; - hasCycle = slow == fast; + if (slow == fast) { + ListNode* ans = head; + while (ans != slow) { + ans = ans->next; + slow = slow->next; + } + return ans; + } } - if (!hasCycle) { - return nullptr; - } - ListNode* p = head; - while (p != slow) { - p = p->next; - slow = slow->next; - } - return p; + return nullptr; } }; ``` @@ -141,20 +154,51 @@ public: * } */ func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast - } - if !hasCycle { - return nil - } - p := head - for p != slow { - p, slow = p.Next, slow.Next + fast, slow := head, head + for fast != nil && fast.Next != nil { + slow = slow.Next + fast = fast.Next.Next + if slow == fast { + ans := head + for ans != slow { + ans = ans.Next + slow = slow.Next + } + return ans + } } - return p + return nil +} +``` + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function detectCycle(head: ListNode | null): ListNode | null { + let [slow, fast] = [head, head]; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } + } + return null; } ``` @@ -172,23 +216,20 @@ func detectCycle(head *ListNode) *ListNode { * @return {ListNode} */ var detectCycle = function (head) { - let slow = head; - let fast = head; - let hasCycle = false; - while (!hasCycle && fast && fast.next) { + let [slow, fast] = [head, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return null; - } - let p = head; - while (p != slow) { - p = p.next; - slow = slow.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - return p; + return null; }; ``` diff --git a/lcci/02.08.Linked List Cycle/Solution.cpp b/lcci/02.08.Linked List Cycle/Solution.cpp index 890916dbc2485..480c200cce75d 100644 --- a/lcci/02.08.Linked List Cycle/Solution.cpp +++ b/lcci/02.08.Linked List Cycle/Solution.cpp @@ -9,22 +9,20 @@ class Solution { public: ListNode* detectCycle(ListNode* head) { - ListNode* slow = head; ListNode* fast = head; - bool hasCycle = false; - while (!hasCycle && fast && fast->next) { + ListNode* slow = head; + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return nullptr; - } - ListNode* p = head; - while (p != slow) { - p = p->next; - slow = slow->next; + if (slow == fast) { + ListNode* ans = head; + while (ans != slow) { + ans = ans->next; + slow = slow->next; + } + return ans; + } } - return p; + return nullptr; } }; \ No newline at end of file diff --git a/lcci/02.08.Linked List Cycle/Solution.go b/lcci/02.08.Linked List Cycle/Solution.go index 29cb7c3eb5848..2a55341dd8464 100644 --- a/lcci/02.08.Linked List Cycle/Solution.go +++ b/lcci/02.08.Linked List Cycle/Solution.go @@ -6,18 +6,18 @@ * } */ func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast + fast, slow := head, head + for fast != nil && fast.Next != nil { + slow = slow.Next + fast = fast.Next.Next + if slow == fast { + ans := head + for ans != slow { + ans = ans.Next + slow = slow.Next + } + return ans + } } - if !hasCycle { - return nil - } - p := head - for p != slow { - p, slow = p.Next, slow.Next - } - return p + return nil } \ No newline at end of file diff --git a/lcci/02.08.Linked List Cycle/Solution.java b/lcci/02.08.Linked List Cycle/Solution.java index 95282d17fb34f..d97ce63041d60 100644 --- a/lcci/02.08.Linked List Cycle/Solution.java +++ b/lcci/02.08.Linked List Cycle/Solution.java @@ -11,21 +11,19 @@ */ public class Solution { public ListNode detectCycle(ListNode head) { - ListNode slow = head, fast = head; - boolean hasCycle = false; - while (!hasCycle && fast != null && fast.next != null) { + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; + if (slow == fast) { + ListNode ans = head; + while (ans != slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - if (!hasCycle) { - return null; - } - ListNode p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; } } \ No newline at end of file diff --git a/lcci/02.08.Linked List Cycle/Solution.js b/lcci/02.08.Linked List Cycle/Solution.js index 409dfa46a3ed7..7546639d0047a 100644 --- a/lcci/02.08.Linked List Cycle/Solution.js +++ b/lcci/02.08.Linked List Cycle/Solution.js @@ -11,21 +11,18 @@ * @return {ListNode} */ var detectCycle = function (head) { - let slow = head; - let fast = head; - let hasCycle = false; - while (!hasCycle && fast && fast.next) { + let [slow, fast] = [head, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - if (!hasCycle) { - return null; - } - let p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; }; diff --git a/lcci/02.08.Linked List Cycle/Solution.py b/lcci/02.08.Linked List Cycle/Solution.py index 381618f41c2d1..e28f848a4834c 100644 --- a/lcci/02.08.Linked List Cycle/Solution.py +++ b/lcci/02.08.Linked List Cycle/Solution.py @@ -6,15 +6,14 @@ class Solution: - def detectCycle(self, head: ListNode) -> ListNode: - slow = fast = head - has_cycle = False - while not has_cycle and fast and fast.next: - slow, fast = slow.next, fast.next.next - has_cycle = slow == fast - if not has_cycle: - return None - p = head - while p != slow: - p, slow = p.next, slow.next - return p + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + fast = slow = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + if slow == fast: + ans = head + while ans != slow: + ans = ans.next + slow = slow.next + return ans diff --git a/lcci/02.08.Linked List Cycle/Solution.ts b/lcci/02.08.Linked List Cycle/Solution.ts new file mode 100644 index 0000000000000..1919f3cb375a8 --- /dev/null +++ b/lcci/02.08.Linked List Cycle/Solution.ts @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function detectCycle(head: ListNode | null): ListNode | null { + let [slow, fast] = [head, head]; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } + } + return null; +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" index 4136f250b7ff1..ef0bea2fcd916 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" @@ -65,7 +65,25 @@ ## 解法 -### 方法一 +### 方法一:快慢指针 + +我们先利用快慢指针判断链表是否有环,如果有环的话,快慢指针一定会相遇,且相遇的节点一定在环中。 + +如果没有环,快指针会先到达链表尾部,直接返回 `null` 即可。 + +如果有环,我们再定义一个答案指针 $ans$ 指向链表头部,然后让 $ans$ 和慢指针一起向前走,每次走一步,直到 $ans$ 和慢指针相遇,相遇的节点即为环的入口节点。 + +为什么这样能找到环的入口节点呢? + +我们不妨假设链表头节点到环入口的距离为 $x$,环入口到相遇节点的距离为 $y$,相遇节点到环入口的距离为 $z$,那么慢指针走过的距离为 $x + y$,快指针走过的距离为 $x + y + k \times (y + z)$,其中 $k$ 是快指针在环中绕了 $k$ 圈。 + +

+ +由于快指针速度是慢指针的 $2$ 倍,因此有 $2 \times (x + y) = x + y + k \times (y + z)$,可以推出 $x + y = k \times (y + z)$,即 $x = (k - 1) \times (y + z) + z$。 + +也即是说,如果我们定义一个答案指针 $ans$ 指向链表头部,然后 $ans$ 和慢指针一起向前走,那么它们一定会在环入口相遇。 + +时间复杂度 $O(n)$,其中 $n$ 是链表中节点的数目。空间复杂度 $O(1)$。 @@ -78,18 +96,17 @@ class Solution: - def detectCycle(self, head: ListNode) -> ListNode: - slow = fast = head - has_cycle = False - while not has_cycle and fast and fast.next: - slow, fast = slow.next, fast.next.next - has_cycle = slow == fast - if not has_cycle: - return None - p = head - while p != slow: - p, slow = p.next, slow.next - return p + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + fast = slow = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + if slow == fast: + ans = head + while ans != slow: + ans = ans.next + slow = slow.next + return ans ``` ```java @@ -106,22 +123,20 @@ class Solution: */ public class Solution { public ListNode detectCycle(ListNode head) { - ListNode slow = head, fast = head; - boolean hasCycle = false; - while (!hasCycle && fast != null && fast.next != null) { + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; + if (slow == fast) { + ListNode ans = head; + while (ans != slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - if (!hasCycle) { - return null; - } - ListNode p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; } } ``` @@ -138,23 +153,21 @@ public class Solution { class Solution { public: ListNode* detectCycle(ListNode* head) { - ListNode* slow = head; ListNode* fast = head; - bool hasCycle = false; - while (!hasCycle && fast && fast->next) { + ListNode* slow = head; + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return nullptr; + if (slow == fast) { + ListNode* ans = head; + while (ans != slow) { + ans = ans->next; + slow = slow->next; + } + return ans; + } } - ListNode* p = head; - while (p != slow) { - p = p->next; - slow = slow->next; - } - return p; + return nullptr; } }; ``` @@ -168,20 +181,51 @@ public: * } */ func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast - } - if !hasCycle { - return nil + fast, slow := head, head + for fast != nil && fast.Next != nil { + slow = slow.Next + fast = fast.Next.Next + if slow == fast { + ans := head + for ans != slow { + ans = ans.Next + slow = slow.Next + } + return ans + } } - p := head - for p != slow { - p, slow = p.Next, slow.Next - } - return p + return nil +} +``` + +```ts +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function detectCycle(head: ListNode | null): ListNode | null { + let [slow, fast] = [head, head]; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } + } + return null; } ``` @@ -199,26 +243,19 @@ func detectCycle(head *ListNode) *ListNode { * @return {ListNode} */ var detectCycle = function (head) { - let slow = head; - let fast = head; - let hasCycle = false; - while (!hasCycle && fast && fast.next) { + let [slow, fast] = [head, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return null; - } - let p = head; - while (p != slow) { - p = p.next; - slow = slow.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - return p; + return null; }; ``` - - - - diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" index 890916dbc2485..480c200cce75d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" @@ -9,22 +9,20 @@ class Solution { public: ListNode* detectCycle(ListNode* head) { - ListNode* slow = head; ListNode* fast = head; - bool hasCycle = false; - while (!hasCycle && fast && fast->next) { + ListNode* slow = head; + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; - hasCycle = slow == fast; - } - if (!hasCycle) { - return nullptr; - } - ListNode* p = head; - while (p != slow) { - p = p->next; - slow = slow->next; + if (slow == fast) { + ListNode* ans = head; + while (ans != slow) { + ans = ans->next; + slow = slow->next; + } + return ans; + } } - return p; + return nullptr; } }; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.go" index 29cb7c3eb5848..2a55341dd8464 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.go" @@ -6,18 +6,18 @@ * } */ func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast + fast, slow := head, head + for fast != nil && fast.Next != nil { + slow = slow.Next + fast = fast.Next.Next + if slow == fast { + ans := head + for ans != slow { + ans = ans.Next + slow = slow.Next + } + return ans + } } - if !hasCycle { - return nil - } - p := head - for p != slow { - p, slow = p.Next, slow.Next - } - return p + return nil } \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.java" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.java" index 95282d17fb34f..d97ce63041d60 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.java" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.java" @@ -11,21 +11,19 @@ */ public class Solution { public ListNode detectCycle(ListNode head) { - ListNode slow = head, fast = head; - boolean hasCycle = false; - while (!hasCycle && fast != null && fast.next != null) { + ListNode fast = head, slow = head; + while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; + if (slow == fast) { + ListNode ans = head; + while (ans != slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - if (!hasCycle) { - return null; - } - ListNode p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; } } \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.js" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.js" index 409dfa46a3ed7..7546639d0047a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.js" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.js" @@ -11,21 +11,18 @@ * @return {ListNode} */ var detectCycle = function (head) { - let slow = head; - let fast = head; - let hasCycle = false; - while (!hasCycle && fast && fast.next) { + let [slow, fast] = [head, head]; + while (fast && fast.next) { slow = slow.next; fast = fast.next.next; - hasCycle = slow == fast; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } } - if (!hasCycle) { - return null; - } - let p = head; - while (p != slow) { - p = p.next; - slow = slow.next; - } - return p; + return null; }; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.py" index 381618f41c2d1..e28f848a4834c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.py" @@ -6,15 +6,14 @@ class Solution: - def detectCycle(self, head: ListNode) -> ListNode: - slow = fast = head - has_cycle = False - while not has_cycle and fast and fast.next: - slow, fast = slow.next, fast.next.next - has_cycle = slow == fast - if not has_cycle: - return None - p = head - while p != slow: - p, slow = p.next, slow.next - return p + def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]: + fast = slow = head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + if slow == fast: + ans = head + while ans != slow: + ans = ans.next + slow = slow.next + return ans diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.ts" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.ts" new file mode 100644 index 0000000000000..1919f3cb375a8 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.ts" @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * val: number + * next: ListNode | null + * constructor(val?: number, next?: ListNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + * } + */ + +function detectCycle(head: ListNode | null): ListNode | null { + let [slow, fast] = [head, head]; + while (fast && fast.next) { + slow = slow.next; + fast = fast.next.next; + if (slow === fast) { + let ans = head; + while (ans !== slow) { + ans = ans.next; + slow = slow.next; + } + return ans; + } + } + return null; +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" index 16a275f1ff174..2ed0e1ded8caa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" @@ -77,7 +77,15 @@ ## 解法 -### 方法一 +### 方法一:双指针 + +我们使用两个指针 $a$, $b$ 分别指向两个链表 $headA$, $headB$。 + +同时遍历链表,当 $a$ 到达链表 $headA$ 的末尾时,重新定位到链表 $headB$ 的头节点;当 $b$ 到达链表 $headB$ 的末尾时,重新定位到链表 $headA$ 的头节点。 + +若两指针相遇,所指向的结点就是第一个公共节点。若没相遇,说明两链表无公共节点,此时两个指针都指向 `null`,返回其中一个即可。 + +时间复杂度 $O(m+n)$,其中 $m$ 和 $n$ 分别是链表 $headA$ 和 $headB$ 的长度。空间复杂度 $O(1)$。 @@ -91,11 +99,11 @@ class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: - cur1, cur2 = headA, headB - while cur1 != cur2: - cur1 = headB if cur1 is None else cur1.next - cur2 = headA if cur2 is None else cur2.next - return cur1 + a, b = headA, headB + while a != b: + a = a.next if a else headB + b = b.next if b else headA + return a ``` ```java @@ -112,12 +120,12 @@ class Solution: */ public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { - ListNode cur1 = headA, cur2 = headB; - while (cur1 != cur2) { - cur1 = cur1 == null ? headB : cur1.next; - cur2 = cur2 == null ? headA : cur2.next; + ListNode a = headA, b = headB; + while (a != b) { + a = a == null ? headB : a.next; + b = b == null ? headA : b.next; } - return cur1; + return a; } } ``` @@ -134,13 +142,12 @@ public class Solution { class Solution { public: ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { - ListNode* cur1 = headA; - ListNode* cur2 = headB; - while (cur1 != cur2) { - cur1 = cur1 ? cur1->next : headB; - cur2 = cur2 ? cur2->next : headA; + ListNode *a = headA, *b = headB; + while (a != b) { + a = a ? a->next : headB; + b = b ? b->next : headA; } - return cur1; + return a; } }; ``` @@ -154,20 +161,20 @@ public: * } */ func getIntersectionNode(headA, headB *ListNode) *ListNode { - cur1, cur2 := headA, headB - for cur1 != cur2 { - if cur1 == nil { - cur1 = headB + a, b := headA, headB + for a != b { + if a == nil { + a = headB } else { - cur1 = cur1.Next + a = a.Next } - if cur2 == nil { - cur2 = headA + if b == nil { + b = headA } else { - cur2 = cur2.Next + b = b.Next } } - return cur1 + return a } ``` @@ -185,13 +192,13 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { */ function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { - let p1: ListNode | null = headA; - let p2: ListNode | null = headB; - while (p1 != p2) { - p1 = p1 == null ? headB : p1.next; - p2 = p2 == null ? headA : p2.next; + let a = headA; + let b = headB; + while (a != b) { + a = a ? a.next : headB; + b = b ? b.next : headA; } - return p1; + return a; } ``` @@ -210,16 +217,42 @@ function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): Li * @return {ListNode} */ var getIntersectionNode = function (headA, headB) { - let cur1 = headA; - let cur2 = headB; - while (cur1 != cur2) { - cur1 = cur1 ? cur1.next : headB; - cur2 = cur2 ? cur2.next : headA; + let a = headA; + let b = headB; + while (a != b) { + a = a ? a.next : headB; + b = b ? b.next : headA; } - return cur1; + return a; }; ``` +```swift +/** + * Definition for singly-linked list. + * public class ListNode { + * public var val: Int + * public var next: ListNode? + * public init(_ val: Int) { + * self.val = val + * self.next = nil + * } + * } + */ + +class Solution { + func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? { + var a = headA + var b = headB + while a !== b { + a = a == nil ? headB : a?.next + b = b == nil ? headA : b?.next + } + return a + } +} +``` + diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" index 753db3cdd199c..6c4f6f2552d78 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" @@ -9,12 +9,11 @@ class Solution { public: ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { - ListNode* cur1 = headA; - ListNode* cur2 = headB; - while (cur1 != cur2) { - cur1 = cur1 ? cur1->next : headB; - cur2 = cur2 ? cur2->next : headA; + ListNode *a = headA, *b = headB; + while (a != b) { + a = a ? a->next : headB; + b = b ? b->next : headA; } - return cur1; + return a; } }; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cs" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cs" new file mode 100644 index 0000000000000..9057d2647a678 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cs" @@ -0,0 +1,18 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * public int val; + * public ListNode next; + * public ListNode(int x) { val = x; } + * } + */ +public class Solution { + public ListNode GetIntersectionNode(ListNode headA, ListNode headB) { + ListNode a = headA, b = headB; + while (a != b) { + a = a == null ? headB : a.next; + b = b == null ? headA : b.next; + } + return a; + } +} diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.go" index 217528bd00f5f..06247a2ab7fe2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.go" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.go" @@ -6,18 +6,18 @@ * } */ func getIntersectionNode(headA, headB *ListNode) *ListNode { - cur1, cur2 := headA, headB - for cur1 != cur2 { - if cur1 == nil { - cur1 = headB + a, b := headA, headB + for a != b { + if a != nil { + a = a.Next } else { - cur1 = cur1.Next + a = headB } - if cur2 == nil { - cur2 = headA + if b != nil { + b = b.Next } else { - cur2 = cur2.Next + b = headA } } - return cur1 + return a } \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.java" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.java" index ee4bb36e17936..997d57e6a205a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.java" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.java" @@ -9,13 +9,13 @@ * } * } */ -public class Solution { - public ListNode getIntersectionNode(ListNode headA, ListNode headB) { - ListNode cur1 = headA, cur2 = headB; - while (cur1 != cur2) { - cur1 = cur1 == null ? headB : cur1.next; - cur2 = cur2 == null ? headA : cur2.next; +class Solution { + ListNode getIntersectionNode(ListNode headA, ListNode headB) { + ListNode a = headA, b = headB; + while (a != b) { + a = a == null ? headB : a.next; + b = b == null ? headA : b.next; } - return cur1; + return a; } } \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.js" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.js" index d1ca97e8900a6..934643598feee 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.js" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.js" @@ -12,11 +12,11 @@ * @return {ListNode} */ var getIntersectionNode = function (headA, headB) { - let cur1 = headA; - let cur2 = headB; - while (cur1 != cur2) { - cur1 = cur1 ? cur1.next : headB; - cur2 = cur2 ? cur2.next : headA; + let a = headA; + let b = headB; + while (a != b) { + a = a ? a.next : headB; + b = b ? b.next : headA; } - return cur1; + return a; }; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.py" index 000c74aac0285..bedd686dd2534 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.py" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.py" @@ -7,8 +7,8 @@ class Solution: def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: - cur1, cur2 = headA, headB - while cur1 != cur2: - cur1 = headB if cur1 is None else cur1.next - cur2 = headA if cur2 is None else cur2.next - return cur1 + a, b = headA, headB + while a != b: + a = a.next if a else headB + b = b.next if b else headA + return a diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.ts" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.ts" index cf53d7d26d9a1..d5321f905bf8c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.ts" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.ts" @@ -11,11 +11,11 @@ */ function getIntersectionNode(headA: ListNode | null, headB: ListNode | null): ListNode | null { - let p1: ListNode | null = headA; - let p2: ListNode | null = headB; - while (p1 != p2) { - p1 = p1 == null ? headB : p1.next; - p2 = p2 == null ? headA : p2.next; + let a = headA; + let b = headB; + while (a != b) { + a = a ? a.next : headB; + b = b ? b.next : headA; } - return p1; + return a; } From a8dda66ba34f98bfb1206a314e590a08f2f58911 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 3 Apr 2024 08:46:26 +0800 Subject: [PATCH 2/2] Update pull_request_template.md --- .github/pull_request_template.md | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 212773f151498..8b137891791fe 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,16 +1 @@ - - \ No newline at end of file