Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Finding Intersection Point of Two Linked Lists
In this tutorial, we will discuss two approaches to finding the intersection point of two linked lists in JavaScript. The first approach uses nested loops, and the second approach uses the difference of nodes technique which works in linear time.
We will be given two linked lists that may intersect at some point. The intersection point is where both lists share the same node reference (not just the same value). After the intersection point, all subsequent nodes are identical in both lists.
Problem Statement
Given two linked lists, we need to find the node where they intersect. If the lists don't intersect, we return null.
Method 1: Nested Loop Approach
This approach uses two nested loops to compare each node of the first list with each node of the second list using reference equality (==).
Example
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
function findIntersectionNested(head1, head2) {
let temp1 = head1;
while (temp1 !== null) {
let temp2 = head2;
while (temp2 !== null) {
if (temp1 === temp2) {
return temp1.value;
}
temp2 = temp2.next;
}
temp1 = temp1.next;
}
return null;
}
// Create common part
let common = new Node(9);
common.next = new Node(5);
common.next.next = new Node(8);
// Create first linked list: 1 -> 3 -> 2 -> common
let head1 = new Node(1);
head1.next = new Node(3);
head1.next.next = new Node(2);
head1.next.next.next = common;
// Create second linked list: 8 -> 1 -> common
let head2 = new Node(8);
head2.next = new Node(1);
head2.next.next = common;
let result = findIntersectionNested(head1, head2);
console.log("Intersection point:", result);
Intersection point: 9
Time and Space Complexity
The Time complexity is O(N×M) where N and M are the lengths of the two linked lists. The Space complexity is O(1) as no extra space is used.
Method 2: Length Difference Approach
This optimized approach first calculates the lengths of both lists, then advances the longer list by the difference. Finally, it traverses both lists simultaneously until an intersection is found.
Example
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
function getLength(head) {
let count = 0;
let temp = head;
while (temp !== null) {
count++;
temp = temp.next;
}
return count;
}
function findIntersectionOptimized(head1, head2) {
let len1 = getLength(head1);
let len2 = getLength(head2);
let difference = Math.abs(len1 - len2);
let temp1 = head1;
let temp2 = head2;
// Advance the longer list by the difference
if (len1 > len2) {
for (let i = 0; i 3 -> 2 -> common
let head1 = new Node(1);
head1.next = new Node(3);
head1.next.next = new Node(2);
head1.next.next.next = common;
// Create second linked list: 8 -> 1 -> common
let head2 = new Node(8);
head2.next = new Node(1);
head2.next.next = common;
let result = findIntersectionOptimized(head1, head2);
console.log("Intersection point:", result);
console.log("Length of list 1:", getLength(head1));
console.log("Length of list 2:", getLength(head2));
Intersection point: 9 Length of list 1: 6 Length of list 2: 5
Time and Space Complexity
The Time complexity is O(N+M) where N and M are the lengths of the lists. The Space complexity is O(1) as no extra space is used.
Comparison
| Method | Time Complexity | Space Complexity | Efficiency |
|---|---|---|---|
| Nested Loop | O(N×M) | O(1) | Less efficient |
| Length Difference | O(N+M) | O(1) | More efficient |
Conclusion
We explored two methods to find intersection points in linked lists. The length difference approach is more efficient with O(N+M) time complexity, making it the preferred solution for large datasets.
