Javascript Program For Finding Intersection Point Of Two Linked Lists
Last Updated :
06 Sep, 2024
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 having 15 as intersection points.
Method 1(Simply use two loops):Â
Use 2 nested for loops. The outer loop will be for each node of the 1st list and the inner loop will be for the 2nd list. In the inner loop, check if any of the nodes of the 2nd list is the same as the current node of the first linked list. The time complexity of this method will be O(M * N) where m and n are the numbers of nodes in two lists.
Method 2 (Mark Visited Nodes):Â
This solution requires modifications to basic linked list data structure. Have a visited flag with each node. Traverse the first linked list and keep marking visited nodes. Now traverse the second linked list, If you see a visited node again then there is an intersection point, return the intersecting node. This solution works in O(m+n) but requires additional information with each node. A variation of this solution that doesn’t require modification to the basic data structure can be implemented using a hash. Traverse the first linked list and store the addresses of visited nodes in a hash. Now traverse the second linked list and if you see an address that already exists in the hash then return the intersecting node.
Method 3(Using difference of node counts):Â
- Get count of the nodes in the first list, let count be c1.
- Get count of the nodes in the second list, let count be c2.
- Get the difference of counts d = abs(c1 – c2)
- Now traverse the bigger list from the first node till d nodes so that from here onwards both the lists have equal no of nodes
- Then we can traverse both the lists in parallel till we come across a common node. (Note that getting a common node is done by comparing the address of the nodes)
Below image is a dry run of the above approach:

Below is the implementation of the above approach :
JavaScript
// Javascript program to implement
// the above approach
class Node {
constructor(item) {
this.data = item;
this.next = null;
}
}
let head1, head2;
function getNode() {
let c1 = getCount(head1);
let c2 = getCount(head2);
let d;
if (c1 > c2) {
d = c1 - c2;
return _getIntesectionNode(d, head1,
head2);
}
else {
d = c2 - c1;
return _getIntesectionNode(d, head2,
head1);
}
}
function _getIntesectionNode(d, node1,
node2) {
let i;
let current1 = node1;
let current2 = node2;
for (i = 0; i < d; i++) {
if (current1 == null) {
return -1;
}
current1 = current1.next;
}
while (current1 != null &&
current2 != null) {
if (current1.data == current2.data) {
return current1.data;
}
current1 = current1.next;
current2 = current2.next;
}
return -1;
}
function getCount(node) {
let current = node;
let count = 0;
while (current != null) {
count++;
current = current.next;
}
return count;
}
head1 = new Node(3);
head1.next = new Node(6);
head1.next.next = new Node(9);
head1.next.next.next = new Node(15);
head1.next.next.next.next = new Node(30);
// Creating second linked list
head2 = new Node(10);
head2.next = new Node(15);
head2.next.next = new Node(30);
console.log("The node of intersection is " +
getNode());
// This code is contributed by avanitrachhadiya2155
OutputThe node of intersection is 15
Complexity Analysis:
- Time Complexity: O(m+n)Â
- Auxiliary Space: O(1)
Method 4(Make circle in first list):Â
Thanks to Saravanan Man for providing below solution.Â
1. Traverse the first linked list(count the elements) and make a circular linked list. (Remember the last node so that we can break the circle later on).Â
2. Now view the problem as finding the loop in the second linked list. So the problem is solved.Â
3. Since we already know the length of the loop(size of the first linked list) we can traverse those many numbers of nodes in the second list, and then start another pointer from the beginning of the second list. we have to traverse until they are equal, and that is the required intersection point.Â
4. remove the circle from the linked list.Â
Time Complexity: O(m+n)Â
Auxiliary Space: O(1)
Method 5 (Reverse the first list and make equations):Â
Thanks to Saravanan Mani for providing this method.
1) Let X be the length of the first linked list until intersection point.
Let Y be the length of the second linked list until the intersection point.
Let Z be the length of the linked list from the intersection point to End of
the linked list including the intersection node.
We Have
X + Z = C1;
Y + Z = C2;
2) Reverse first linked list.
3) Traverse Second linked list. Let C3 be the length of second list - 1.
Now we have
X + Y = C3
We have 3 linear equations. By solving them, we get
X = (C1 + C3 – C2)/2;
Y = (C2 + C3 – C1)/2;
Z = (C1 + C2 – C3)/2;
WE GOT THE INTERSECTION POINT.
4) Reverse first linked list.
Advantage: No Comparison of pointers.Â
Disadvantage: Modifying linked list(Reversing list).Â
Time complexity: O(m+n)Â
Auxiliary Space: O(1)
Method 6 (Traverse both lists and compare addresses of last nodes): This method is only to detect if there is an intersection point or not. (Thanks to NeoTheSaviour for suggesting this)Â Â
1) Traverse list 1, store the last node address
2) Traverse list 2, store the last node address.
3) If nodes stored in 1 and 2 are same then they are intersecting.
The time complexity of this method is O(m+n) and used Auxiliary space is O(1)
Method 7 (Use Hashing):Â
Basically, we need to find a common node of two linked lists. So we hash all nodes of the first list and then check the second list.Â
1) Create an empty hash set.Â
2) Traverse the first linked list and insert all nodes’ addresses in the hash set.Â
3) Traverse the second list. For every node check if it is present in the hash set. If we find a node in the hash set, return the node.
JavaScript
// Javascript program to get intersection
// point of two linked list
class Node {
constructor(d) {
this.data = d;
this.next = null;
}
}
// Function to print the list
function Print(n) {
let cur = n;
while (cur != null) {
console.log(cur.data + " ");
cur = cur.next;
}
}
// Function to find the intersection
// of two node
function MegeNode(n1, n2) {
// Define hashset
let hs = new Set();
while (n1 != null) {
hs.add(n1);
n1 = n1.next;
}
while (n2 != null) {
if (hs.has(n2)) {
return n2;
}
n2 = n2.next;
}
return null;
}
// Driver code
// List 1
let n1 = new Node(1);
n1.next = new Node(2);
n1.next.next = new Node(3);
n1.next.next.next = new Node(4);
n1.next.next.next.next = new Node(5);
n1.next.next.next.next.next = new Node(6);
n1.next.next.next.next.next.next = new Node(7);
// List 2
let n2 = new Node(10);
n2.next = new Node(9);
n2.next.next = new Node(8);
n2.next.next.next = n1.next.next.next;
Print(n1);
Print(n2);
console.log(MegeNode(n1, n2).data);
// This code is contributed by rag2127
Output:
1 2 3 4 5 6 7
10 9 8 4 5 6 7
4
This method required O(n) additional space and not very efficient if one list is large.
Time complexity: O(m+n) where m is the size of linked list 1 and n is the size of linked list 2
Please refer complete article on Write a function to get the intersection point of two Linked Lists for more details!
Similar Reads
JavaScript Linked List Programs
JavaScript Linked List Programs contain a list of articles based on programming. Linked List is a linear data structure that stores data in linearly connected nodes. Linked lists store elements sequentially, but doesnât store the elements contiguously like an array. S. NoArticles1JavaScript Program
5 min read
Implementation of LinkedList in Javascript
In this article, we will be implementing the LinkedList data structure in Javascript. A linked list is a linear data structure where elements are stored in nodes, each containing a value and a reference (or pointer) to the next node. It allows for efficient insertion and deletion operations. Each no
5 min read
Javascript Program For Searching An Element In A Linked List
Write a function that searches a given key 'x' in a given singly linked list. The function should return true if x is present in linked list and false otherwise. bool search(Node *head, int x) For example, if the key to be searched is 15 and linked list is 14->21->11->30->10, then functi
3 min read
Javascript Program For Inserting A Node In A Linked List
We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of the linked list. [GFGTABS] JavaScript // Linked List Class // Head of list let he
7 min read
Javascript Program For Inserting Node In The Middle Of The Linked List
Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3Output : 1->2->3
4 min read
Javascript Program For Writing A Function To Delete A Linked List
A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers. This article focuses on writing a function to delete a linked list. Implementation: [GFGTABS] JavaScript // Javascript program to de
2 min read
Javascript Program For Deleting A Linked List Node At A Given Position
Given a singly linked list and a position, delete a linked list node at the given position. Example: Input: position = 1, Linked List = 8->2->3->1->7Output: Linked List = 8->3->1->7Input: position = 0, Linked List = 8->2->3->1->7Output: Linked List = 2->3->1-
3 min read
Javascript Program For Finding Length Of A Linked List
Write a function to count the number of nodes in a given singly linked list. For example, the function should return 5 for linked list 1->3->1->2->1. Iterative Solution: 1) Initialize count as 0 2) Initialize a node pointer, current = head.3) Do following while current is not NULL a) cur
3 min read
Javascript Program For Rotating A Linked List
Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smal
5 min read
Javascript Program For Making Middle Node Head In A Linked List
Given a singly linked list, find middle of the linked list and set middle node of the linked list at beginning of the linked list. Examples: Input: 1 2 3 4 5 Output: 3 1 2 4 5Input: 1 2 3 4 5 6Output: 4 1 2 3 5 6 The idea is to first find middle of a linked list using two pointers, first one moves o
3 min read