Java Program For Finding Intersection Of Two Sorted Linked Lists
Last Updated :
07 Aug, 2022
Given two lists sorted in increasing order, create and return a new list representing the intersection of the two lists. The new list should be made with its own memory — the original lists should not be changed.
Example:
Input:
First linked list: 1->2->3->4->6
Second linked list be 2->4->6->8,
Output: 2->4->6.
The elements 2, 4, 6 are common in
both the list so they appear in the
intersection list.
Input:
First linked list: 1->2->3->4->5
Second linked list be 2->3->4,
Output: 2->3->4
The elements 2, 3, 4 are common in
both the list so they appear in the
intersection list.
Method 1: Using Dummy Node.
Approach:
The idea is to use a temporary dummy node at the start of the result list. The pointer tail always points to the last node in the result list, so new nodes can be added easily. The dummy node initially gives the tail a memory space to point to. This dummy node is efficient, since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either ‘a’ or ‘b’ and adding it to the tail. When the given lists are traversed the result is in dummy. next, as the values are allocated from next node of the dummy. If both the elements are equal then remove both and insert the element to the tail. Else remove the smaller element among both the lists.
Below is the implementation of the above approach:
Java
class GFG
{
static Node a = null , b = null ;
static Node dummy = null ;
static Node tail = null ;
static class Node
{
int data;
Node next;
Node( int data)
{
this .data = data;
next = null ;
}
}
void printList(Node start)
{
Node p = start;
while (p != null )
{
System.out.print(p.data + " " );
p = p.next;
}
System.out.println();
}
void push( int data)
{
Node temp = new Node(data);
if (dummy == null )
{
dummy = temp;
tail = temp;
}
else
{
tail.next = temp;
tail = temp;
}
}
void sortedIntersect()
{
Node p = a,q = b;
while (p != null && q != null )
{
if (p.data == q.data)
{
push(p.data);
p = p.next;
q = q.next;
}
else if (p.data < q.data)
p = p.next;
else
q= q.next;
}
}
public static void main(String args[])
{
GFG list = new GFG();
list.a = new Node( 1 );
list.a.next = new Node( 2 );
list.a.next.next = new Node( 3 );
list.a.next.next.next = new Node( 4 );
list.a.next.next.next.next = new Node( 6 );
list.b = new Node( 2 );
list.b.next = new Node( 4 );
list.b.next.next = new Node( 6 );
list.b.next.next.next = new Node( 8 );
list.sortedIntersect();
System.out.println(
"Linked list containing common items of a & b" );
list.printList(dummy);
}
}
|
Output:
Linked list containing common items of a & b
2 4 6
Complexity Analysis:
- Time Complexity: O(m+n) where m and n are number of nodes in first and second linked lists respectively.
Only one traversal of the lists are needed.
- Auxiliary Space: O(min(m, n)).
The output list can store at most min(m,n) nodes .
Method 2: Use Hashing
Java
import java.util.*;
public class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node( int d)
{
data = d;
next= null ;
}
}
public void printList()
{
Node n = head;
while (n != null )
{
System.out.println(n.data + " " );
n = n.next;
}
}
public void append( int d)
{
Node n = new Node(d);
if (head== null )
{
head = new Node(d);
return ;
}
n.next = null ;
Node last = head;
while (last.next != null )
{
last = last.next;
}
last.next = n;
return ;
}
static int [] intersection(Node tmp1,
Node tmp2, int k)
{
int [] res = new int [k];
HashSet<Integer> set = new HashSet<Integer>();
while (tmp1 != null )
{
set.add(tmp1.data);
tmp1 = tmp1.next;
}
int cnt = 0 ;
while (tmp2 != null )
{
if (set.contains(tmp2.data))
{
res[cnt] = tmp2.data;
cnt++;
}
tmp2 = tmp2.next;
}
return res;
}
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
LinkedList ll1 = new LinkedList();
ll.append( 0 );
ll.append( 1 );
ll.append( 2 );
ll.append( 3 );
ll.append( 4 );
ll.append( 5 );
ll.append( 6 );
ll.append( 7 );
ll1.append( 9 );
ll1.append( 0 );
ll1.append( 12 );
ll1.append( 3 );
ll1.append( 4 );
ll1.append( 5 );
ll1.append( 6 );
ll1.append( 7 );
int [] arr= intersection(ll.head,
ll1.head, 6 );
for ( int i : arr)
{
System.out.println(i);
}
}
|
Output:
0
3
4
5
6
7
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n) as using extra space
Please refer complete article on Intersection of two Sorted Linked Lists for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...