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:
CPP
// 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
// 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 |
C#
// 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 |
8 -> 5 ->
Time Complexity: O(M * N), where M and N are the lengths of the two given linked list.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Find the common nodes in two singly linked list
- Find count of common nodes in two Doubly Linked Lists
- Remove all Fibonacci Nodes from a Circular Singly Linked List
- Remove all the Even Digit Sum Nodes from a Circular Singly Linked List
- Remove all even parity nodes from a Doubly and Circular Singly Linked List
- Longest common suffix of two linked lists
- First common element in two linked lists
- Remove all special characters from a singly Linked List
- Reverse alternate K nodes in a Singly Linked List
- Alternate Odd and Even Nodes in a Singly Linked List
- Delete all Prime Nodes from a Singly Linked List
- Sum of the nodes of a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Sum and Product of all Prime Nodes of a Singly Linked List
- Product of the nodes of a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Count of Prime Nodes of a Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Delete all Non-Prime Nodes from a Singly Linked List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

