|
| 1 | +# [160.相交链表](https://leetcode-cn.com/problems/intersection-of-two-linked-lists/description/) |
| 2 | + |
| 3 | + |
| 4 | +### 题目描述 |
| 5 | + |
| 6 | +<div class="notranslate"><p>编写一个程序,找到两个单链表相交的起始节点。</p> |
| 7 | + |
| 8 | +<p>如下面的两个链表<strong>:</strong></p> |
| 9 | + |
| 10 | +<p><a href="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png"><img style="height: 130px; width: 400px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_statement.png" alt=""></a></p> |
| 11 | + |
| 12 | +<p>在节点 c1 开始相交。</p> |
| 13 | + |
| 14 | +<p> </p> |
| 15 | + |
| 16 | +<p><strong>示例 1:</strong></p> |
| 17 | + |
| 18 | +<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_1.png"><img style="height: 130px; width: 400px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_1.png" alt=""></a></p> |
| 19 | + |
| 20 | +<pre><strong>输入:</strong>intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 |
| 21 | +<strong>输出:</strong>Reference of the node with value = 8 |
| 22 | +<strong>输入解释:</strong>相交节点的值为 8 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。 |
| 23 | +</pre> |
| 24 | + |
| 25 | +<p> </p> |
| 26 | + |
| 27 | +<p><strong>示例 2:</strong></p> |
| 28 | + |
| 29 | +<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_2.png"><img style="height: 136px; width: 350px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_2.png" alt=""></a></p> |
| 30 | + |
| 31 | +<pre><strong>输入:</strong>intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 |
| 32 | +<strong>输出:</strong>Reference of the node with value = 2 |
| 33 | +<strong>输入解释:</strong>相交节点的值为 2 (注意,如果两个链表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。 |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | + |
| 38 | +<p><strong>示例 3:</strong></p> |
| 39 | + |
| 40 | +<p><a href="https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png"><img style="height: 126px; width: 200px;" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/14/160_example_3.png" alt=""></a></p> |
| 41 | + |
| 42 | +<pre><strong>输入:</strong>intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 |
| 43 | +<strong>输出:</strong>null |
| 44 | +<strong>输入解释:</strong>从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。 |
| 45 | +<strong>解释:</strong>这两个链表不相交,因此返回 null。 |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | + |
| 50 | +<p><strong>注意:</strong></p> |
| 51 | + |
| 52 | +<ul> |
| 53 | + <li>如果两个链表没有交点,返回 <code>null</code>.</li> |
| 54 | + <li>在返回结果后,两个链表仍须保持原有的结构。</li> |
| 55 | + <li>可假定整个链表结构中没有循环。</li> |
| 56 | + <li>程序尽量满足 O(<em>n</em>) 时间复杂度,且仅用 O(<em>1</em>) 内存。</li> |
| 57 | +</ul> |
| 58 | +</div> |
| 59 | + |
| 60 | +### 解题思路 |
| 61 | + |
| 62 | +``` |
| 63 | +链表A和链表B设置指针A和指针B,然后开始遍历链表,如果遍历完当前链表,则将指针指向另外一个链表的头部继续遍历,直至两个指针相遇。 |
| 64 | + |
| 65 | +最终两个指针分别走过的路径为: |
| 66 | + |
| 67 | +指针A:a+c+b |
| 68 | + |
| 69 | +指针B :b+c+a |
| 70 | + |
| 71 | +明显 a+c+b = b+c+a,因而如果两个链表相交,则指针A和指针B必定在相交结点相遇。 |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +### 代码实现 |
| 77 | + |
| 78 | +<!-- tabs:start --> |
| 79 | + |
| 80 | +#### **Golang** |
| 81 | +```go |
| 82 | +type ListNode struct { |
| 83 | + Val int |
| 84 | + Next *ListNode |
| 85 | +} |
| 86 | + |
| 87 | +func getIntersectionNode(headA, headB *ListNode) *ListNode { |
| 88 | + p, q := headA, headB |
| 89 | + for p != q { |
| 90 | + if p == nil { |
| 91 | + p = headA |
| 92 | + } else { |
| 93 | + p = p.Next |
| 94 | + } |
| 95 | + if q == nil { |
| 96 | + q = headB |
| 97 | + } else { |
| 98 | + q = q.Next |
| 99 | + } |
| 100 | + } |
| 101 | + return p |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | + |
| 106 | +<!-- tabs:end --> |
0 commit comments