# Problem description

Here are the head nodes headA and headB of the two single linked lists. Please find and return the starting node where the two single linked lists intersect. If two linked lists have no intersection, null is returned.

As shown in the figure, two linked lists intersect at node c1:

The title data ensures that there are no rings in the whole chain structure.

Note that after the function returns the result, the linked list must maintain its original structure.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: Intersected at '8' Explanation: the value of intersection node is 8 (note that if two linked lists intersect, it cannot be 0). The linked list starts from the respective header A by [4,1,8,4,5]，Linked list B by [5,0,1,8,4,5]. stay A In, there are 2 nodes before the intersecting node; stay B In, there are 3 nodes before the intersection node.

Example 2:

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1 Output: Intersected at '2' Explanation: the value of intersection node is 2 (note that if two linked lists intersect, it cannot be 0). The linked list starts from the respective header A by [0,9,1,2,4]，Linked list B by [3,2,4]. stay A In, there are 3 nodes before the intersection node; stay B In, there is 1 node before the intersection node.

Example 3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2 Output: null Explanation: the linked list starts from the respective header A by [2,6,4]，Linked list B by [1,5]. Because the two linked lists do not intersect, so intersectVal Must be 0, and skipA and skipB Can be any value. The two linked lists do not intersect, so they return null .

Tips:

- The number of nodes in listA is m
- The number of nodes in listB is n
- 0 <= m, n <= 3 * 104
- 1 <= Node.val <= 105
- 0 <= skipA <= m
- 0 <= skipB <= n
- If listA and listB have no intersection, intersectVal is 0
- If listA and listB have an intersection, intersectVal == listA[skipA + 1] == listB[skipB + 1]

Advanced: can you design a solution with time complexity O(n) and only O(1) memory?

Source: LeetCode

Link: https://leetcode-cn.com/problems/intersection-of-two-linked-lists

The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

# Method I: double pointer

code:

class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if (headA == nullptr || headB == nullptr) {//If one is empty, it must not intersect return nullptr; } ListNode* curA = headA; ListNode* curB = headB; while (curA != curB) { curA = curA == nullptr ? headB : curA->next; curB = curB == nullptr ? headA : curB->next; } return curA; } };

Execution results:

analysis:

The solution of this problem is still very subtle. We can consider it in two cases: 1. Two linked lists intersect; 2. Two linked lists do not intersect

## 1. Two linked lists intersect

We assume that the length of the first linked list is x+m, where x is the disjoint x nodes and M is the intersecting m nodes. Similarly, the length of the second linked list is y+m. Where x, Y > = 0, M > 0.

We set two pointers to point to headA and headB respectively, and the two pointers keep pointing back. If they reach the end of the linked list, they point to the head node of another linked list. Then when the number of nodes passed by the two pointers is x+m+y (y+m+x), the two pointers will be equal. Of course, there is a special case x==y, so the pointer is equal before it reaches the end of the linked list. So we can write code.

## 2. The two linked lists do not intersect

If it does not intersect, let's set the length of the two linked lists as the same, then we know x, Y > = 0, m=0. Then, when the number of nodes passed by the two pointers is x+y (y+x), the two pointers will point to null at the same time. At this time, we also think that the two pointers are equal, so we don't need special consideration.

Here we show another example of the error code:

class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { if (headA == nullptr || headB == nullptr) {//If one is empty, it must not intersect return nullptr; } ListNode* curA = headA; ListNode* curB = headB; while (curA != curB) { curA = curA == nullptr ? headB : curA->next; curB = curB == nullptr ? headA : curB->next; if (curA == headA) { return nullptr; } } return curA; } };

Different from the correct code, this code considers the situation that the two linked lists do not intersect, that is, it adds such a code to the loop: