|
| 1 | +/* |
| 2 | +Intersection of Two Linked Lists |
| 3 | +================================ |
| 4 | +
|
| 5 | +Write a program to find the node at which the intersection of two singly linked lists begins. |
| 6 | +
|
| 7 | +Example 1: |
| 8 | +Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3 |
| 9 | +Output: Reference of the node with value = 8 |
| 10 | +Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). 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. |
| 11 | +
|
| 12 | +Example 2: |
| 13 | +Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 |
| 14 | +Output: Reference of the node with value = 2 |
| 15 | +Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). 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. |
| 16 | +
|
| 17 | +Example 3: |
| 18 | +Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 |
| 19 | +Output: null |
| 20 | +Input 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. |
| 21 | +Explanation: The two lists do not intersect, so return null. |
| 22 | +
|
| 23 | +Notes: |
| 24 | +If the two linked lists have no intersection at all, return null. |
| 25 | +The linked lists must retain their original structure after the function returns. |
| 26 | +You may assume there are no cycles anywhere in the entire linked structure. |
| 27 | +Each value on each linked list is in the range [1, 10^9]. |
| 28 | +Your code should preferably run in O(n) time and use only O(1) memory. |
| 29 | +*/ |
| 30 | + |
| 31 | +/** |
| 32 | + * Definition for singly-linked list. |
| 33 | + * struct ListNode { |
| 34 | + * int val; |
| 35 | + * ListNode *next; |
| 36 | + * ListNode(int x) : val(x), next(NULL) {} |
| 37 | + * }; |
| 38 | + */ |
| 39 | + |
| 40 | +class Solution |
| 41 | +{ |
| 42 | +public: |
| 43 | + ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) |
| 44 | + { |
| 45 | + int sizeA = 0, sizeB = 0; |
| 46 | + auto tempA = headA, tempB = headB; |
| 47 | + |
| 48 | + while (tempA) |
| 49 | + { |
| 50 | + sizeA++; |
| 51 | + tempA = tempA->next; |
| 52 | + } |
| 53 | + while (tempB) |
| 54 | + { |
| 55 | + sizeB++; |
| 56 | + tempB = tempB->next; |
| 57 | + } |
| 58 | + |
| 59 | + tempA = headA; |
| 60 | + tempB = headB; |
| 61 | + |
| 62 | + while (sizeA > sizeB) |
| 63 | + { |
| 64 | + tempA = tempA->next; |
| 65 | + sizeA--; |
| 66 | + } |
| 67 | + while (sizeB > sizeA) |
| 68 | + { |
| 69 | + tempB = tempB->next; |
| 70 | + sizeB--; |
| 71 | + } |
| 72 | + |
| 73 | + while (tempA != tempB) |
| 74 | + { |
| 75 | + tempA = tempA->next; |
| 76 | + tempB = tempB->next; |
| 77 | + } |
| 78 | + |
| 79 | + return tempA; |
| 80 | + } |
| 81 | +}; |
0 commit comments