Find Intersection Point of Two Linked Lists in JavaScript



In this tutorial, we will discuss two approaches to finding the intersection point of two linked lists in JavaScript. The first approach involves using the loops, and the second approach involves using the difference of nodes technique which works in the linear time.

We will be given two linked lists that are not sorted. Note that there is another version of this problem in which we are given a sorted pair of linked lists and have to find the intersection. We have to find the elements after which all the elements in both linked lists are the same. We will provide a proper code with an explanation.

Introduction to Problem

In this problem, we are given two linked lists and both contain some numbers in an unsorted manner we have to find the numbers after which all the elements in both Linked Lists are the same.

For example ?

Input

List1: 1 -> 3 -> 2 -> 9 -> 3 -> 5 -> 8
List2: 8 -> 1 -> 4 -> 3 -> 5 -> 8 

Output

3

From the given linked lists, we have common points 3, 5, and 8, and it starts from 3, so we will return 3 as the answer.

If none of the nodes is like this present after that node all the elements including the current node are equal, then we will return null as the result.

Nested Loop Method

In this approach, we can use two Nested loops, and using both loops we can traverse over the linked lists and check if they are the same or not. We will define two linked lists and for each of them, we will add a common linked list at the end to get it using the loops.

Defining the common linked list ?

var common = new Node(3)
common.next = new Node(5)
common.next.next = new Node(8)

Printing the linked list ?

function print(head){
var temp = head
while(temp != null) {
console.log(temp.value)
temp = temp.next

Example

Below is an example of finding the Intersection Point of Two Linked Lists ?

class Node{
   constructor(data){
      this.value = data
      this.next = null
   }
}

// printing the linked list
function print(head){
   var temp = head    
   while(temp != null) {
      console.log(temp.value)
      temp = temp.next
   }
}
function intersection(head1, head2){
   var temp1 = head1
   while(temp1 != null){
      var temp2 = head2
      while(temp2 != null){
         if(temp1 == temp2){
            console.log("The intersection point is: " + temp2.value)
            return
         }
         temp2 = temp2.next
      }
      temp1 = temp1.next
   }
   console.log("There is no intersection point")
}

// defining common linked list 
var common = new Node(3)
common.next = new Node(5)
common.next.next = new Node(8)

// defining the first linked list
var head1 = new Node(1)
head1.next = new Node(3)
head1.next.next = new Node(2)
head1.next.next.next = new Node(9)
head1.next.next.next.next = common

// defining the second linked list
var head2 = new Node(8)
head2.next = new Node(1)
head2.next.next = new Node(4)
head2.next.next.next = common

// finding the intersection point
intersection(head1, head2)

Output

The intersection point is: 3

Time and Space Complexity

The Time complexity of the above code is O(N*M) where N is the size of the first linked list and M is the size of the second linked list. The Space complexity of the above code is O(1) as we are not using any extra space here.

Using Difference of Nodes

In this method, we will get the size of both the linked lists and then calculate the difference between the nodes of both linked lists. Then we will move the largest one to the different number of nodes forward and then we will check for each node that they are equal or not.

Getting differences between both linked lists ?

var difference = length(head1) - length(head2)

Example

Below is an example of finding the Intersection Point of Two Linked Lists using a difference of nodes ?

class Node{
   constructor(data){
      this.value = data
      this.next = null
   }
}

// printing the linked list
function print(head){
   var temp = head    
   while(temp != null){
      console.log(temp.value)
      temp = temp.next
   }
}
// finding length of the Linked lists 
function length(head){
   var temp = head
   var count = 0
   while(temp != null){
      count++;
      temp = temp.next
   } 
   return count
}
function intersection(head1, head2, diffrence){
   var temp1 = head1
   var temp2 = head2
   
   // moving first linked list 
   while(diffrence != 0){
      diffrence --
      temp1 = temp1.next;
   }
   while(temp1 != null) {
      if(temp1 == temp2){
         console.log("The intersection point is: " + temp2.value)
         return
      }
      temp1 = temp1.next
      temp2 = temp2.next
   }
   console.log("There is no intersection point")
}
// defining common linked list 
var common = new Node(3)
common.next = new Node(5)
common.next.next = new Node(8)

// defining the first linked list
var head1 = new Node(1)
head1.next = new Node(3)
head1.next.next = new Node(2)
head1.next.next.next = new Node(9)
head1.next.next.next.next = common

// defining the second linked list
var head2 = new Node(8)
head2.next = new Node(1)
head2.next.next = new Node(4)
head2.next.next.next = common

// getting differences of the both linked lists 
var difference = length(head1) - length(head2)

// finding the intersection point
intersection(head1, head2, difference)

Output

The intersection point is: 3

Time and Space Complexity

The Time complexity of the above code is O(N+M), where N is the number of elements present in the first linked list and M is the number of elements present in the second linked list. The Space complexity of the above code is O(1) as we are not using any extra space.

There are some other approaches present such as using the hash maps, making the circles in the first linked list, and traversing over from the last node for both the linked lists. These approaches also work in the linear time complexity.

Conclusion

In this tutorial, we have implemented a JavaScript program for finding the intersection point of two linked lists. We have given two linked lists that are not going to be sorted and we have to find the element after which all the elements are the same in both linked lists. We have seen two approaches one is by using the loops and another one is by using the difference of nodes technique which works in the linear time.

Updated on: 2024-12-27T19:17:04+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements