1. Method 1(Simply use two loops)
Use 2 nested for loops. The outer loop will be for each node of the 1st list and inner loop will be for 2nd list. In the inner loop, check if any of nodes of the 2nd list is same as the current node of the first linked list. The time complexity of this method will be O(mn) where m and n are the numbers of nodes in two lists.
2. Method 2(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)
// Java program to get intersection point of two linked list
class LinkedList {
static Node head1, head2;
static class Node {
int data;
Node next;
Node(int d) {
data = d;
next = null;
}
}
/*function to get the intersection point of two linked
lists head1 and head2 */
int getNode() {
int c1 = getCount(head1);
int c2 = getCount(head2);
int d;
if (c1 > c2) {
d = c1 - c2;
return _getIntesectionNode(d, head1, head2);
} else {
d = c2 - c1;
return _getIntesectionNode(d, head2, head1);
}
}
/* function to get the intersection point of two linked
lists head1 and head2 where head1 has d more nodes than
head2 */
int _getIntesectionNode(int d, Node node1, Node node2) {
int i;
Node current1 = node1;
Node 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;
}
/*Takes head pointer of the linked list and
returns the count of nodes in the list */
int getCount(Node node) {
Node current = node;
int count = 0;
while (current != null) {
count++;
current = current.next;
}
return count;
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
// creating first linked list
list.head1 = new Node(3);
list.head1.next = new Node(6);
list.head1.next.next = new Node(15);
list.head1.next.next.next = new Node(15);
list.head1.next.next.next.next = new Node(30);
// creating second linked list
list.head2 = new Node(10);
list.head2.next = new Node(15);
list.head2.next.next = new Node(30);
System.out.println("The node of intersection is " + list.getNode());
}
}
// This code has been contributed by Mayank Jaiswal
Output:The node of intersection is 15
Time Complexity: O(m+n)
Auxiliary Space: O(1)
3. Method 3 (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.
- Create an empty hash set.
- Traverse the first linked list and insert all nodes’ addresses in the
hash set. - 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.
// Java program to get intersection point of two linked list
import java.util.*;
class Node{
int data;
Node next;
Node(int d){
data=d;
next=null;
}
}
class LinkedListIntersect{
public static void main(String[] args) {
//list 1
Node 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
Node 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);
System.out.println(MegeNode(n1,n2).data);
}
//function to print the list
public static void Print(Node n){
Node cur=n;
while(cur!=null){
System.out.print(cur.data+" ");
cur=cur.next;
}
System.out.println();
}
//function to find the intersection of two node
public static Node MegeNode(Node n1,Node n2){
//define hashset
HashSet<Node> hs=new HashSet<Node>();
while(n1!=null){
hs.add(n1);
n1=n1.next;
}
while(n2!=null){
if(hs.contains(n2)){
return n2;
}
n2=n2.next;
}
return null;
}
}
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