From 730f58bccee54b576c2b5bc998e5b97fe4fb5567 Mon Sep 17 00:00:00 2001 From: hongyang <1664698982@qq.com> Date: Thu, 9 Jun 2022 23:24:30 +0800 Subject: [PATCH 1/2] refactor: add golang solution to Intersection of Two Linked Lists LCCI --- problems/面试题02.07.链表相交.md | 53 ++++++++++++++++++------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index 0a38cc33..f603925d 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -13,21 +13,21 @@ 图示两个链表在节点 c1 开始相交: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221657.png) 题目数据 保证 整个链式结构中不存在环。 -注意,函数返回结果后,链表必须 保持其原始结构 。 +注意,函数返回结果后,链表必须 保持其原始结构 。 -示例 1: +示例 1: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221723.png) 示例 2: -![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) +![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221749.png) -示例 3: +示例 3: ![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png)![](https://code-thinking-1253855093.file.myqcloud.com/pics/20211219221812.png) @@ -100,7 +100,7 @@ public: ## 其他语言版本 -### Java +### Java ```Java public class Solution { public ListNode getIntersectionNode(ListNode headA, ListNode headB) { @@ -144,11 +144,11 @@ public class Solution { } return null; } - + } ``` -### Python +### Python ```python class Solution: @@ -162,15 +162,15 @@ class Solution: """ cur_a, cur_b = headA, headB # 用两个指针代替a和b - + while cur_a != cur_b: cur_a = cur_a.next if cur_a else headB # 如果a走完了,那么就切换到b走 cur_b = cur_b.next if cur_b else headA # 同理,b走完了就切换到a - + return cur_a ``` -### Go +### Go ```go func getIntersectionNode(headA, headB *ListNode) *ListNode { @@ -208,7 +208,30 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -### javaScript +递归版本: + +```go +func getIntersectionNode(headA, headB *ListNode) *ListNode { + l1,l2 := headA, headB + for l1 != l2 { + if l1 != nil { + l1 = l1.Next + } else { + l1 = headB + } + + if l2 != nil { + l2 = l2.Next + } else { + l2 = headA + } + } + + return l1 +} +``` + +### javaScript ```js var getListLen = function(head) { @@ -218,9 +241,9 @@ var getListLen = function(head) { cur = cur.next; } return len; -} +} var getIntersectionNode = function(headA, headB) { - let curA = headA,curB = headB, + let curA = headA,curB = headB, lenA = getListLen(headA), lenB = getListLen(headB); if(lenA < lenB) { From 26f5e59cdaf5536e0b08b94d554e4ef4428732a7 Mon Sep 17 00:00:00 2001 From: leo <55868230+LIU-HONGYANG@users.noreply.github.com> Date: Sat, 16 Jul 2022 11:56:18 +0800 Subject: [PATCH 2/2] =?UTF-8?q?Update=20=E9=9D=A2=E8=AF=95=E9=A2=9802.07.?= =?UTF-8?q?=E9=93=BE=E8=A1=A8=E7=9B=B8=E4=BA=A4.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/面试题02.07.链表相交.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/problems/面试题02.07.链表相交.md b/problems/面试题02.07.链表相交.md index f603925d..d799bc80 100644 --- a/problems/面试题02.07.链表相交.md +++ b/problems/面试题02.07.链表相交.md @@ -208,7 +208,7 @@ func getIntersectionNode(headA, headB *ListNode) *ListNode { } ``` -递归版本: +双指针 ```go func getIntersectionNode(headA, headB *ListNode) *ListNode {