Given two Linked Lists L1 and L2, the task is to generate a new linked list with no common elements from the given two linked lists.
Example:
Input: L1 = 10 -> 15 -> 5 -> 20, L2 = 8 -> 5 -> 20 -> 10
Output: 8 -> 15
Explanation:
Since both the linked list has 5, 10 and 20 in common. Therefore these elements are removed and the resultant list is 8 -> 15.Input: L1 = 0 -> 5 -> 52 -> 21, L2 = 21 -> 5 -> 0 -> 52
Output: [ ]
Explanation:
Since all the elements of the two given linked list are common. So the resultant linked is empty.
Approach:
- For each element(say X) in the first linked list:
- Traverse the second linked list and check if X is present in the linked list or not.
- If X is not present, then insert X in the resultant linked list as it not common in both linked list.
- For each element(say Y) in the second linked list:
- Traverse the first linked list and check if Y is present in the linked list or not.
- If Y is not present, then insert Y in the resultant linked list as it not common in both linked list.
- The resultant linked list will be the required linked list with no nodes common from the given two linked list.
Below is the implementation of the above approach:
// C++ program for the above approach #include <bits/stdc++.h> using namespace std;
// Link list node struct Node {
int data;
struct Node* next;
}; // Function to print the element // of the Linked List void printList( struct Node* p)
{ if (p == NULL) {
cout << "[]" ;
}
while (p != NULL) {
cout << p->data << " -> " ;
p = p->next;
}
} // Function to push the node at the // beginning of the linked list void push( struct Node** head_ref,
int new_data)
{ struct Node* new_node
= ( struct Node*) malloc (
sizeof ( struct Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
} // Function to insert unique // elements in the new LL void traverse( struct Node** head3,
struct Node* temp1,
struct Node* temp2)
{ // Traverse the first linked list
while (temp1 != NULL) {
// Value of current node
int val = temp1->data;
struct Node* t = temp2;
int x = 0;
// Traverse the second list
while (t != NULL) {
if (t->data == val) {
x = 1;
break ;
}
t = t->next;
}
// If element is not common
// then insert it in the
// resultant linked list
if (x == 0) {
push(head3, temp1->data);
}
temp1 = temp1->next;
}
} // Function to remove the common nodes // in two Singly Linked Lists void removeCommonNodes( struct Node* head1,
struct Node* head2)
{ // Head pointer for the resultant
// linked list
struct Node* head3 = NULL;
// Find the node common between
// linked list taking head1 as
// first linked list
traverse(&head3, head1, head2);
// Find the node common between
// linked list taking head2 as
// first linked list
traverse(&head3, head2, head1);
// Print the resultant linked list
printList(head3);
} // Driver code int main()
{ // First list
struct Node* head1 = NULL;
push(&head1, 20);
push(&head1, 5);
push(&head1, 15);
push(&head1, 10);
// Second list
struct Node* head2 = NULL;
push(&head2, 10);
push(&head2, 20);
push(&head2, 15);
push(&head2, 8);
// Function call
removeCommonNodes(head1, head2);
return 0;
} |
// Java program for the above approach class GFG{
// Link list node static class Node {
int data;
Node next;
}; // Function to print the element // of the Linked List static void printList(Node p)
{ if (p == null ) {
System.out.print( "[]" );
}
while (p != null ) {
System.out.print(p.data+ "->" );
p = p.next;
}
} // Function to push the node at the // beginning of the linked list static Node push(Node head_ref,
int new_data)
{ Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
} // Function to insert unique // elements in the new LL static Node traverse(Node head3,
Node temp1,
Node temp2)
{ // Traverse the first linked list
while (temp1 != null ) {
// Value of current node
int val = temp1.data;
Node t = temp2;
int x = 0 ;
// Traverse the second list
while (t != null ) {
if (t.data == val) {
x = 1 ;
break ;
}
t = t.next;
}
// If element is not common
// then insert it in the
// resultant linked list
if (x == 0 ) {
head3 = push(head3, temp1.data);
}
temp1 = temp1.next;
}
return head3;
} // Function to remove the common nodes // in two Singly Linked Lists static void removeCommonNodes(Node head1,
Node head2)
{ // Head pointer for the resultant
// linked list
Node head3 = null ;
// Find the node common between
// linked list taking head1 as
// first linked list
head3 = traverse(head3, head1, head2);
// Find the node common between
// linked list taking head2 as
// first linked list
head3 = traverse(head3, head2, head1);
// Print the resultant linked list
printList(head3);
} // Driver code public static void main(String[] args)
{ // First list
Node head1 = new Node();
head1 = push(head1, 20 );
head1 = push(head1, 5 );
head1 = push(head1, 15 );
head1 = push(head1, 10 );
// Second list
Node head2 = new Node();
head2 = push(head2, 10 );
head2 = push(head2, 20 );
head2 = push(head2, 15 );
head2 = push(head2, 8 );
// Function call
removeCommonNodes(head1, head2);
} } // This code is contributed by sapnasingh4991 |
# Python3 program for the above approach # Link list node class Node:
def __init__( self ):
self .data = 0
self . next = None
# Function to print the element # of the Linked List def printList(p):
if (p = = None ):
print ( '[]' , end = '')
while (p ! = None ):
print (p.data, end = ' -> ' )
p = p. next
# Function to push the node at the # beginning of the linked list def push(head_ref, new_data):
new_node = Node()
new_node.data = new_data
new_node. next = (head_ref)
(head_ref) = new_node
return head_ref
# Function to insert unique # elements in the new LL def traverse(head3, temp1, temp2):
# Traverse the first linked list
while (temp1 ! = None ):
# Value of current node
val = temp1.data
t = temp2
x = 0
# Traverse the second list
while (t ! = None ):
if (t.data = = val):
x = 1
break
t = t. next
# If element is not common
# then insert it in the
# resultant linked list
if (x = = 0 ):
head3 = push(head3, temp1.data)
temp1 = temp1. next
return head3
# Function to remove the common nodes # in two Singly Linked Lists def removeCommonNodes(head1, head2):
# Head pointer for the resultant
# linked list
head3 = None
# Find the node common between
# linked list taking head1 as
# first linked list
head3 = traverse(head3, head1, head2)
# Find the node common between
# linked list taking head2 as
# first linked list
head3 = traverse(head3, head2, head1)
# Print the resultant linked list
printList(head3)
# Driver code if __name__ = = '__main__' :
# First list
head1 = None
head1 = push(head1, 20 )
head1 = push(head1, 5 )
head1 = push(head1, 15 )
head1 = push(head1, 10 )
# Second list
head2 = None
head2 = push(head2, 10 )
head2 = push(head2, 20 )
head2 = push(head2, 15 )
head2 = push(head2, 8 )
# Function call
removeCommonNodes(head1, head2)
# This code is contributed by rutvik_56 |
// C# program for the above approach using System;
class GFG{
// Link list node class Node {
public int data;
public Node next;
}; // Function to print the element // of the Linked List static void printList(Node p)
{ if (p == null ) {
Console.Write( "[]" );
}
while (p != null ) {
Console.Write(p.data+ "->" );
p = p.next;
}
} // Function to push the node at the // beginning of the linked list static Node push(Node head_ref,
int new_data)
{ Node new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
} // Function to insert unique // elements in the new LL static Node traverse(Node head3,
Node temp1,
Node temp2)
{ // Traverse the first linked list
while (temp1 != null ) {
// Value of current node
int val = temp1.data;
Node t = temp2;
int x = 0;
// Traverse the second list
while (t != null ) {
if (t.data == val) {
x = 1;
break ;
}
t = t.next;
}
// If element is not common
// then insert it in the
// resultant linked list
if (x == 0) {
head3 = push(head3, temp1.data);
}
temp1 = temp1.next;
}
return head3;
} // Function to remove the common nodes // in two Singly Linked Lists static void removeCommonNodes(Node head1,
Node head2)
{ // Head pointer for the resultant
// linked list
Node head3 = null ;
// Find the node common between
// linked list taking head1 as
// first linked list
head3 = traverse(head3, head1, head2);
// Find the node common between
// linked list taking head2 as
// first linked list
head3 = traverse(head3, head2, head1);
// Print the resultant linked list
printList(head3);
} // Driver code public static void Main(String[] args)
{ // First list
Node head1 = new Node();
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
// Second list
Node head2 = new Node();
head2 = push(head2, 10);
head2 = push(head2, 20);
head2 = push(head2, 15);
head2 = push(head2, 8);
// Function call
removeCommonNodes(head1, head2);
} } // This code is contributed by PrinciRaj1992 |
<script> // javascript program for the above approach // Link list node class Node { constructor() {
this .data = 0;
this .next = null ;
}
} // Function to print the element
// of the Linked List
function printList(p) {
if (p == null ) {
document.write( "" );
}
while (p != null ) {
document.write(p.data + "->" );
p = p.next;
}
}
// Function to push the node at the
// beginning of the linked list
function push(head_ref , new_data) {
var new_node = new Node();
new_node.data = new_data;
new_node.next = head_ref;
head_ref = new_node;
return head_ref;
}
// Function to insert unique
// elements in the new LL
function traverse(head3, temp1, temp2) {
// Traverse the first linked list
while (temp1 != null ) {
// Value of current node
var val = temp1.data;
var t = temp2;
var x = 0;
// Traverse the second list
while (t != null ) {
if (t.data == val) {
x = 1;
break ;
}
t = t.next;
}
// If element is not common
// then insert it in the
// resultant linked list
if (x == 0) {
head3 = push(head3, temp1.data);
}
temp1 = temp1.next;
}
return head3;
}
// Function to remove the common nodes
// in two Singly Linked Lists
function removeCommonNodes(head1, head2) {
// Head pointer for the resultant
// linked list
var head3 = null ;
// Find the node common between
// linked list taking head1 as
// first linked list
head3 = traverse(head3, head1, head2);
// Find the node common between
// linked list taking head2 as
// first linked list
head3 = traverse(head3, head2, head1);
// Print the resultant linked list
printList(head3);
}
// Driver code
// First list
var head1 = new Node();
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
// Second list
var head2 = new Node();
head2 = push(head2, 10);
head2 = push(head2, 20);
head2 = push(head2, 15);
head2 = push(head2, 8);
// Function call
removeCommonNodes(head1, head2);
// This code contributed by aashish1995 </script> |
8 -> 5 ->
Time Complexity: O(M * N), where M and N are the lengths of the two given linked list.
Auxiliary Space: O(1) because it is using constant space