304 North Cardinal St.
Dorchester Center, MA 02124

# Intersection of Two Linked Lists LeetCode Solution

## Problem – Intersection of Two Linked Lists

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`.

For example, the following two linked lists begin to intersect at node `c1`:

The test cases are generated such that there are no cycles anywhere in the entire linked structure.

Note that the linked lists must retain their original structure after the function returns.

Custom Judge:

The inputs to the judge are given as follows (your program is not given these inputs):

• `intersectVal` – The value of the node where the intersection occurs. This is `0` if there is no intersected node.
• `listA` – The first linked list.
• `listB` – The second linked list.
• `skipA` – The number of nodes to skip ahead in `listA` (starting from the head) to get to the intersected node.
• `skipB` – The number of nodes to skip ahead in `listB` (starting from the head) to get to the intersected node.

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.

Example 1:

``````Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,6,1,8,4,5], skipA = 2, skipB = 3
Output: Intersected at '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.``````

Example 2:

``````Input: intersectVal = 2, listA = [1,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Intersected at '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.``````

Example 3:

``````Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: No intersection
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.
Explanation: The two lists do not intersect, so return null.``````

Constraints:

• The number of nodes of `listA` is in the `m`.
• The number of nodes of `listB` is in the `n`.
• `1 <= m, n <= 3 * 104`
• `1 <= Node.val <= 105`
• `0 <= skipA < m`
• `0 <= skipB < n`
• `intersectVal` is `0` if `listA` and `listB` do not intersect.
• `intersectVal == listA[skipA] == listB[skipB]` if `listA` and `listB` intersect.

### Intersection of Two Linked Lists LeetCode Solution in Java

``````public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
//boundary check

//if a & b have different len, then we will stop the loop after second iteration
while( a != b){
//for the end of first iteration, we just reset the pointer to the head of another linkedlist
a = a == null? headB : a.next;
b = b == null? headA : b.next;
}

return a;
}
``````

### Intersection of Two Linked Lists LeetCode Solution in C++

``````ListNode *getIntersectionNode(ListNode *headA, ListNode *headB)
{

if (p1 == NULL || p2 == NULL) return NULL;

while (p1 != NULL && p2 != NULL && p1 != p2) {
p1 = p1->next;
p2 = p2->next;

//
// Any time they collide or reach end together without colliding
// then return any one of the pointers.
//
if (p1 == p2) return p1;

//
// If one of them reaches the end earlier then reuse it
// by moving it to the beginning of other list.
// Once both of them go through reassigning,
// they will be equidistant from the collision point.
//
if (p1 == NULL) p1 = headB;
if (p2 == NULL) p2 = headA;
}

return p1;
}
``````

### Intersection of Two Linked Lists LeetCode Solution in Python

``````class Solution:
# @param two ListNodes
# @return the intersected ListNode
return None

pa = headA # 2 pointers

while pa is not pb:
# if either pointer hits the end, switch head and continue the second traversal,
# if not hit the end, just move on to next
pa = headB if pa is None else pa.next
pb = headA if pb is None else pb.next

return pa # only 2 ways to get out of the loop, they meet or the both hit the end=None

# the idea is if you switch head, the possible difference between length would be countered.
# On the second traversal, they either hit or miss.
# if they meet, pa or pb would be the node we are looking for,
# if they didn't meet, they will hit the end at the same iteration, pa == pb == None, return either one of them is the same,None
``````
##### Intersection of Two Linked Lists LeetCode Solution Review:

In our experience, we suggest you solve this Intersection of Two Linked Lists LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Intersection of Two Linked Lists LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Intersection of Two Linked Lists LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions