|
| 1 | +# 160. Intersection of Two Linked Lists (鍊表相交) |
| 2 | + |
| 3 | +[參考來源](https://github.com/youngyangyang04/leetcode-master/blob/master/problems/%E9%9D%A2%E8%AF%95%E9%A2%9802.07.%E9%93%BE%E8%A1%A8%E7%9B%B8%E4%BA%A4.md) |
| 4 | + |
| 5 | +**題目** |
| 6 | + |
| 7 | +Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect. If the two linked lists have no intersection at all, return null. |
| 8 | + |
| 9 | +For example, the following two linked lists begin to intersect at node c1 |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +The test cases are generated such that there are no cycles anywhere in the entire linked structure. |
| 14 | + |
| 15 | +Note that the linked lists must retain their original structure after the function returns. |
| 16 | + |
| 17 | +Custom Judge: |
| 18 | + |
| 19 | +The inputs to the judge are given as follows (your program is not given these inputs): |
| 20 | + |
| 21 | +- intersectVal - The value of the node where the intersection occurs. This is 0 if there is no intersected node. |
| 22 | +- listA - The first linked list. |
| 23 | +- listB - The second linked list. |
| 24 | +- skipA - The number of nodes to skip ahead in listA (starting from the head) to get to the intersected node. |
| 25 | +- skipB - The number of nodes to skip ahead in listB (starting from the head) to get to the intersected node. |
| 26 | + |
| 27 | +The judge will then create the linked structure based on these inputs and pass the two heads, headA and headB to your program. If you correctly return the intersected node, then your solution will be accepted. |
| 28 | + |
| 29 | + |
| 30 | +**ex1** |
| 31 | + |
| 32 | + |
| 33 | + |
| 34 | + Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 |
| 35 | + Output: Intersected at '8' |
| 36 | + Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). |
| 37 | + From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,6,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B. |
| 38 | + - Note that the intersected node's value is not 1 because the nodes with value 1 in A and B (2nd node in A and 3rd node in B) are different node references. In other words, they point to two different locations in memory, while the nodes with value 8 in A and B (3rd node in A and 4th node in B) point to the same location in memory. |
| 39 | + |
| 40 | +**ex2** |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 |
| 45 | + Output: Intersected at '2' |
| 46 | + Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). |
| 47 | + From the head of A, it reads as [1,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B. |
| 48 | + |
| 49 | +**ex3** |
| 50 | + |
| 51 | + |
| 52 | + |
| 53 | + Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 |
| 54 | + Output: No intersection |
| 55 | + Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values. |
| 56 | + Explanation: The two lists do not intersect, so return null. |
| 57 | + |
| 58 | +Constraints:\ |
| 59 | +- The number of nodes of listA is in the m. |
| 60 | +- The number of nodes of listB is in the n. |
| 61 | +- 1 <= m, n <= 3 * 104 |
| 62 | +- 1 <= Node.val <= 105 |
| 63 | +- 0 <= skipA < m |
| 64 | +- 0 <= skipB < n |
| 65 | +- intersectVal is 0 if listA and listB do not intersect. |
| 66 | +- intersectVal == listA[skipA] == listB[skipB] if listA and listB intersect. |
| 67 | + |
| 68 | + ## 解題思路 |
| 69 | + |
| 70 | +- 題目意在求出兩個鍊表交點節點的指針,而不是數值! |
| 71 | +- step1: 找出兩個鍊表的長度 |
| 72 | +- step2: 將常鍊表的指針與短鍊表對齊 |
| 73 | +- step3: 同時移動並比較下一個節點是否相同 |
| 74 | +- step4: 相同則返回鍊表,若不相同則返回NULL |
| 75 | + |
| 76 | +**程式碼** |
| 77 | + |
| 78 | + struct ListNode *getIntersectionNode(struct ListNode *headA, struct ListNode *headB) { |
| 79 | + typedef struct ListNode ListNode; |
| 80 | + ListNode *curA = headA; |
| 81 | + ListNode *curB = headB; |
| 82 | + int lenA=0, lenB=0; |
| 83 | + //計算鍊表長度 |
| 84 | + while(curA != NULL){ |
| 85 | + lenA++; |
| 86 | + curA = curA->next; |
| 87 | + } |
| 88 | + while(curB != NULL){ |
| 89 | + lenB++; |
| 90 | + curB = curB->next; |
| 91 | + } |
| 92 | + |
| 93 | + curA = headA; |
| 94 | + curB = headB; |
| 95 | + int gap; |
| 96 | + // 讓lenA為最長的鍊表,比較長度、計算差值 |
| 97 | + if (lenA > lenB){ |
| 98 | + gap = lenA - lenB; |
| 99 | + } |
| 100 | + else{ |
| 101 | + ListNode *temp = curA; |
| 102 | + curA = curB; |
| 103 | + curB = temp; |
| 104 | + gap = lenB - lenA; |
| 105 | + } |
| 106 | + //移動cur |
| 107 | + while(gap--){ |
| 108 | + curA = curA->next; |
| 109 | + } |
| 110 | + //遍歷鍊表,遇到相同則返回,遇到不同則返回NULL |
| 111 | + while(curA != NULL){ |
| 112 | + if (curA == curB){ |
| 113 | + return curA; |
| 114 | + } |
| 115 | + curA = curA->next; |
| 116 | + curB = curB->next; |
| 117 | + } |
| 118 | + return NULL; |
| 119 | + } |
0 commit comments