- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Program for Finding Intersection Point of Two Linked Lists
In this tutorial, we will discuss two approaches to find the intersection point of two linked lists. 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 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 −
If the given linked lists are: List1: 1 -> 3 -> 2 -> 9 -> 3 -> 5 -> 8 List2: 8 -> 1 -> 4 -> 3 -> 5 -> 8 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 that 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 both are 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. Let us see the code −
Example
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)
Time and Space Complexity
The time complexity of the above code is O(N*M) where N is the size of 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 the both linked lists.
Then we will move the largest one to the difference number of nodes forward and then we will check for each node that they are equal or not.
By using this technique we can reduce the time complexity to linear. Let’s see its code −
Example
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)
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 works 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 same in both linked lists. We have seen two approaches one is the by using the loops and another one is by using the difference of nodes technique which works in the linear time.