Solution: Intersection of Two Linked Lists
Tags: algorithms, javascript, java, python
Published: 2021-03-04 09:56:24

*This is part of a series of Leetcode solution explanations ([index](https://dev.to/seanpgallivan/leetcode-solutions-index-57fl)). If you liked this solution or found it useful,* ***please like*** *this post and/or* ***upvote*** *[my solution post on Leetcode's forums](https://leetcode.com/problems/intersection-of-two-linked-lists/discuss/1092898).*

---

#### [Leetcode Problem #160 (*Easy*): Intersection of Two Linked Lists]()

---

#### ***Description:***

<br />(*Jump to*: [*Solution Idea*](#idea) || *Code*: [*JavaScript*](#javascript-code) | [*Python*](#python-code) | [*Java*](#java-code) | [*C++*](#c-code))

>
Write a program to find the node at which the intersection of two singly linked lists begins.
>
For example, the following two linked lists:
>
![Description Visual](https://assets.leetcode.com/uploads/2018/12/13/160_statement.png)
>
begin to intersect at node c1.

---

#### ***Examples:***
>
Example 1:||
|---:|---|
Input:|intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output:|Reference of the node with value = 8
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.
Visual:|![Example 1 Visual](https://assets.leetcode.com/uploads/2020/06/29/160_example_1_1.png)
>
Example 2:||
|---:|---|
Input:|intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output:|Reference of the node with value = 2
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.
Visual:|![Example 1 Visual](https://assets.leetcode.com/uploads/2020/06/29/160_example_2.png)
>
Example 3:||
|---:|---|
Input:|intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output:|null
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. The two lists do not intersect, so return null.
Visual:|![Example 1 Visual](https://assets.leetcode.com/uploads/2018/12/13/160_example_3.png)

---

#### ***Constraints:***
>
 - If the two linked lists have no intersection at all, return `null`.
 - The linked lists must retain their original structure after the function returns.
 - You may assume there are no cycles anywhere in the entire linked structure.
 - Each value on each linked list is in the range `[1, 10^9]`.
 - Your code should preferably run in `O(n)` time and use only `O(1)` memory.

---

#### ***Idea:***

<br />(*Jump to*: [*Problem Description*](#description) || *Code*: [*JavaScript*](#javascript-code) | [*Python*](#python-code) | [*Java*](#java-code) | [*C++*](#c-code))

The naive approach here would be to store each node reference in a data structure until we saw the same one twice, but that would take **O(N) extra space**.

In order to solve this problem with only **O(1) extra space**, we'll need to find another way to align the two linked lists. More importantly, we need to find a way to line up the *ends* of the two lists. And the easiest way to do that is to concatenate them in opposite orders, **A+B** and **B+A**. This way, the ends of the two original lists will align on the second half of each merged list.

![Visual 1](https://i.imgur.com/hcpocCV.png)

![Visual 2](https://i.imgur.com/dDUjSPk.png)

Then we just need to check if at some point the two merged lists are pointing to the same node. In fact, even if the two merged lists don't intersect, the value of **a** and **b** will be the same (**null**) when we come to the end of the merged lists, so we can use that as our exit condition.

We just need to make sure to string **headB** onto **a** and vice versa if one (but not both) list ends.

---

#### ***Implementation:***

There code for all four languages is almost identical.

---

#### ***Javascript Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```javascript
var getIntersectionNode = function(headA, headB) {
    let a = headA, b = headB
    while (a !== b) {
        a = !a ? headB : a.next
        b = !b ? headA : b.next
    }
    return a
};
```

---

#### ***Python Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```python
class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        a, b = headA, headB
        while (a != b):
            a = headB if not a else a.next
            b = headA if not b else b.next
        return a
```

---

#### ***Java Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```java
public class Solution {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode a = headA, b = headB;
        while (a != b) {
            a = a == null ? headB : a.next;
            b = b == null ? headA : b.next;
        }
        return a;
    }
}
```

---

#### ***C++ Code:***
<br />(*Jump to*: [*Problem Description*](#description) || [*Solution Idea*](#idea))
```c++
class Solution {
public:
    ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        ListNode *a = headA, *b = headB;
        while (a != b) {
            a = !a ? headB : a->next;
            b = !b ? headA : b->next;
        }
        return a;
    }
};
```