JavaScript Program for Finding Intersection of Two Sorted Linked Lists


In this program, we are given two linked lists and we have to create a new linked list that will contain all the elements which are common in both of the lists as the lists are given sorted then we can use the concept of two pointers which are going to see in the article below.

Introduction to Problem

In the given problem we have to find the intersection of the given linked lists. Intersection means to get the common values from the given set of values, so here we have given two sorted linked lists from which we have to find the elements which are common in both. We have to return a new linked list and not change the values of the given linked lists.

For example −

We are given two sorted linked lists −

List1: 1 -> 2 -> 4 -> 8 -> 9 -> 11 -> null
List2: 3-> 4 -> 5 -> 7 -> 8 -> 10 -> 11 -> 15 -> 18 -> null

From the above given linked lists, we have common values 4, 8, and 11. So we have to return a sorted linked list: 4 -> 8 -> 11 -> null

If none of the values is the same in both of the linked lists then we have to return an empty linked list that is just an empty node or null value.

Approach

We have seen the example above for the current problem, in this problem we are going to use the concept of the two pointers. Let’s see each step one by one then we will move to the implementation of the code −

  • First, we will create a class that will make the linked list structure and will help to bind the data and the pointer of the next node.

  • Then we will create three head nodes for the given linked lists and the new list to store the answer. Also, we will create a tail node to add the values in the answer-linked list.

  • We will create a function to travel over the linked list to print all the values of the linked list at once.

  • There will be a push function to add elements to the linked list that will store the answer or the extra linked list.

  • The intersection function will be used to implement the main concept which is the two-pointer approach.

  • We will create the given linked lists and then call the intersection function, and later print the final linked list.

Two Pointer Concept

Example

In the given problems we have given linked lists in a sorted manner, so if the current number of both the linked lists is equal then we can say that is the intersection of the given linked lists and if that number is not equal then the linked list with the smaller value must have to move forward so that matching value could be found and if and list reaches the end of the null value then we will terminate our search.

var heada = null 
var headb = null 
var tail = null 
var extra = null 
// creating a class for developing the linked list nodes  
class Node{
   // constructor to initiate a new node 
   constructor(data){
      this.value = data;
      this.next = null;
   }
}
// Function for printing the complete list
function print(head) {
   var temp = head
   var values = 0
   while (temp != null) {
      values = values + temp.value + " -> ";
      temp = temp.next;
   }
   console.log(values + "null");
}
// Inserting elements into the linked list 
function add(val) {
   var temp_node = new Node(val);
   if (extra == null) {
      extra = temp_node;
      tail = temp_node;
   } 
   else {
      tail.next = temp_node;
      tail = temp_node;
   }
}
// Function for finding the intersection 
// and adding it to the extra list
function intersection() {
   // temporary pointers 
   var tempa = heada 
   var tempb = headb 
   while (tempa != null && tempb != null) {
      if (tempa.value == tempb.value){
         // Add to extra list
         add(tempa.value);
         tempa = tempa.next;
         tempb = tempb.next;
      } 
      else if (tempa.value < tempb.value){
         tempa = tempa.next;
      }else
      tempb = tempb.next;
   }
}
// creating the first linked list 
heada = new Node(1);
heada.next = new Node(2);
heada.next.next = new Node(3);
heada.next.next.next = new Node(4);
heada.next.next.next.next = new Node(6);
// Creating the second linked list
headb = new Node(2);
headb.next = new Node(4);
headb.next.next = new Node(6);
headb.next.next.next = new Node(8);
// Function call for intersection
intersection();
// printing the final answer 
console.log("The linked list containing the common items of the linked list is: ");
print(extra);

Time and Space Complexity

The time complexity of the above code is O(N) where N is the size of the linked list because we are iterating over the linked lists using the two pointers.

The space complexity of the above code is O(1). We are using the extra space here but that extra space is to store the final answer, hence that is not the extra which makes the space complexity constant.

Conclusion

In this tutorial, we have implemented a JavaScript program for finding the intersection of two sorted linked lists. We were given two linked lists and we have to create a new linked list that will contain all the elements which are common in both of the lists as the lists are given sorted then we can use the concept of two pointers. The time complexity of our approach is O(N) where N is the size of the linked lists while the space complexity of the given approach is O(1).

Updated on: 24-Mar-2023

106 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements