Get the Intersection point of two Linked List by counting nodes
Last Updated :
28 Nov, 2022
There are two singly linked lists in a system. By some programming error, the end node of one of the linked list got linked to the second list, forming an inverted Y shaped list. Write a program to get the point where two linked list merge.
Above diagram shows an example with two linked list having 15 as intersection point.
Approach: It can be observed that the number of nodes in traversing the first linked list and then from the head of the second linked list to intersection point is equal to the number of nodes involved in traversing the second linked list and then from head of the first list to the intersection point. Considering the example given above, start traversing the two linked lists with two pointers curr1 and curr2 pointing to the heads of the given linked lists respectively.
- If curr1 != null then update it to point to the next node, else it is updated to point to the first node of the second list.
- If curr2 != null then update it to point to the next node, else it is updated to point to the first node of the first list.
- Repeat the above steps while curr1 is not equal to curr2.
The two pointers curr1 and curr2 will be pointing to the same node now i.e. the merging point.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach
#include <iostream>
using namespace std;
// Link list node
struct Node {
int data;
Node* next;
};
// Function to get the intersection point
// of the given linked lists
int getIntersectionNode(Node* head1, Node* head2)
{
Node *curr1 = head1, *curr2 = head2;
// While both the pointers are not equal
while (curr1 != curr2) {
// If the first pointer is null then
// set it to point to the head of
// the second linked list
if (curr1 == NULL) {
curr1 = head2;
}
// Else point it to the next node
else {
curr1 = curr1->next;
}
// If the second pointer is null then
// set it to point to the head of
// the first linked list
if (curr2 == NULL) {
curr2 = head1;
}
// Else point it to the next node
else {
curr2 = curr2->next;
}
}
// Return the intersection node
return curr1->data;
}
// Driver code
int main()
{
/*
Create two linked lists
1st Linked list is 3->6->9->15->30
2nd Linked list is 10->15->30
15 is the intersection point
*/
Node* newNode;
Node* head1 = new Node();
head1->data = 10;
Node* head2 = new Node();
head2->data = 3;
newNode = new Node();
newNode->data = 6;
head2->next = newNode;
newNode = new Node();
newNode->data = 9;
head2->next->next = newNode;
newNode = new Node();
newNode->data = 15;
head1->next = newNode;
head2->next->next->next = newNode;
newNode = new Node();
newNode->data = 30;
head1->next->next = newNode;
head1->next->next->next = NULL;
// Print the intersection node
cout << getIntersectionNode(head1, head2);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Link list node
static class Node
{
int data;
Node next;
};
// Function to get the intersection point
// of the given linked lists
static int getIntersectionNode(Node head1, Node head2)
{
Node curr1 = head1, curr2 = head2;
// While both the pointers are not equal
while (curr1 != curr2)
{
// If the first pointer is null then
// set it to point to the head of
// the second linked list
if (curr1 == null)
{
curr1 = head2;
}
// Else point it to the next node
else
{
curr1 = curr1.next;
}
// If the second pointer is null then
// set it to point to the head of
// the first linked list
if (curr2 == null)
{
curr2 = head1;
}
// Else point it to the next node
else
{
curr2 = curr2.next;
}
}
// Return the intersection node
return curr1.data;
}
// Driver code
public static void main(String[] args)
{
/*
Create two linked lists
1st Linked list is 3.6.9.15.30
2nd Linked list is 10.15.30
15 is the intersection point
*/
Node newNode;
Node head1 = new Node();
head1.data = 10;
Node head2 = new Node();
head2.data = 3;
newNode = new Node();
newNode.data = 6;
head2.next = newNode;
newNode = new Node();
newNode.data = 9;
head2.next.next = newNode;
newNode = new Node();
newNode.data = 15;
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node();
newNode.data = 30;
head1.next.next = newNode;
head1.next.next.next = null;
// Print the intersection node
System.out.print(getIntersectionNode(head1, head2));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Link list node
class Node:
def __init__(self):
self.data = 0
self.next = None
# Function to get the intersection point
# of the given linked lists
def getIntersectionNode( head1, head2):
curr1 = head1
curr2 = head2
# While both the pointers are not equal
while (curr1 != curr2):
# If the first pointer is None then
# set it to point to the head of
# the second linked list
if (curr1 == None) :
curr1 = head2
# Else point it to the next node
else:
curr1 = curr1.next
# If the second pointer is None then
# set it to point to the head of
# the first linked list
if (curr2 == None):
curr2 = head1
# Else point it to the next node
else:
curr2 = curr2.next
# Return the intersection node
return curr1.data
# Driver code
# Create two linked lists
# 1st Linked list is 3.6.9.15.30
# 2nd Linked list is 10.15.30
# 15 is the intersection point
newNode = None
head1 = Node()
head1.data = 10
head2 = Node()
head2.data = 3
newNode = Node()
newNode.data = 6
head2.next = newNode
newNode = Node()
newNode.data = 9
head2.next.next = newNode
newNode = Node()
newNode.data = 15
head1.next = newNode
head2.next.next.next = newNode
newNode = Node()
newNode.data = 30
head1.next.next = newNode
head1.next.next.next = None
# Print the intersection node
print( getIntersectionNode(head1, head2))
# This code is contributed by Arnab Kundu
C#
// C# implementation of the approach
using System;
class GFG
{
// Link list node
public class Node
{
public int data;
public Node next;
};
// Function to get the intersection point
// of the given linked lists
static int getIntersectionNode(Node head1,
Node head2)
{
Node curr1 = head1, curr2 = head2;
// While both the pointers are not equal
while (curr1 != curr2)
{
// If the first pointer is null then
// set it to point to the head of
// the second linked list
if (curr1 == null)
{
curr1 = head2;
}
// Else point it to the next node
else
{
curr1 = curr1.next;
}
// If the second pointer is null then
// set it to point to the head of
// the first linked list
if (curr2 == null)
{
curr2 = head1;
}
// Else point it to the next node
else
{
curr2 = curr2.next;
}
}
// Return the intersection node
return curr1.data;
}
// Driver code
public static void Main(String[] args)
{
/*
Create two linked lists
1st Linked list is 3.6.9.15.30
2nd Linked list is 10.15.30
15 is the intersection point
*/
Node newNode;
Node head1 = new Node();
head1.data = 10;
Node head2 = new Node();
head2.data = 3;
newNode = new Node();
newNode.data = 6;
head2.next = newNode;
newNode = new Node();
newNode.data = 9;
head2.next.next = newNode;
newNode = new Node();
newNode.data = 15;
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node();
newNode.data = 30;
head1.next.next = newNode;
head1.next.next.next = null;
// Print the intersection node
Console.Write(getIntersectionNode(head1, head2));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript implementation of the approach
// Link list node
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
// Function to get the intersection point
// of the given linked lists
function getIntersectionNode(head1, head2)
{
var curr1 = head1, curr2 = head2;
// While both the pointers are not equal
while (curr1 != curr2)
{
// If the first pointer is null then
// set it to point to the head of
// the second linked list
if (curr1 == null)
{
curr1 = head2;
}
// Else point it to the next node
else
{
curr1 = curr1.next;
}
// If the second pointer is null then
// set it to point to the head of
// the first linked list
if (curr2 == null)
{
curr2 = head1;
}
// Else point it to the next node
else
{
curr2 = curr2.next;
}
}
// Return the intersection node
return curr1.data;
}
// Driver Code
/*
Create two linked lists
1st Linked list is 3.6.9.15.30
2nd Linked list is 10.15.30
15 is the intersection point
*/
var newNode;
var head1 = new Node();
head1.data = 10;
var head2 = new Node();
head2.data = 3;
newNode = new Node();
newNode.data = 6;
head2.next = newNode;
newNode = new Node();
newNode.data = 9;
head2.next.next = newNode;
newNode = new Node();
newNode.data = 15;
head1.next = newNode;
head2.next.next.next = newNode;
newNode = new Node();
newNode.data = 30;
head1.next.next = newNode;
head1.next.next.next = null;
// Print the intersection node
document.write(getIntersectionNode(head1, head2));
// This code is contributed by jana_sayantan.
</script>
Time Complexity: O(N + M).
Auxiliary Space: O(1).
Similar Reads
C++ Program For Finding Intersection Point Of Two Linked Lists There are two singly linked lists in a system. By some programming error, the end node of one of the linked lists got linked to the second list, forming an inverted Y-shaped list. Write a program to get the point where two linked lists merge. Above diagram shows an example with two linked lists hav
9 min read
C++ Program For Finding Intersection Of Two Sorted Linked Lists Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory â the original lists should not be changed. Example: Input: First linked list: 1->2->3->4->6 Second linked list be 2-
6 min read
C++ Program For Union And Intersection Of Two Linked Lists Write a C++ program for a given two Linked Lists, create union and intersection lists that contain union and intersection of the elements present in the given lists. The order of elements in output lists doesn't matter.Example: Input:List1: 10->15->4->20List2: 8->4->2->10Output:Int
9 min read
Count of intersections of M line segments with N vertical lines in XY plane Given x coordinates of N vertical lines (parallel to Y-axis) and M line segments extending from (x1, y1) to (x2, y2), the task is to find the total number of intersections of the line segments with the vertical lines. Examples: Input: N = 2, M = 1, lines[] = {-1, 1}, Segments[][4] = {0, 1, 2, 1} Out
15+ min read
C++ Program For Writing A Function To Get Nth Node In A Linked List Write a C++ Program to GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position. Example: Input: 1->10->30->14, index = 2Output: 30 The node at index 2 is 30Recommended: Please solve it on "PRACTICE" first, before m
4 min read