Remove the common nodes in two Singly Linked Lists
Last Updated :
30 Aug, 2022
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++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
void printList( struct Node* p)
{
if (p == NULL) {
cout << "[]" ;
}
while (p != NULL) {
cout << p->data << " -> " ;
p = p->next;
}
}
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;
}
void traverse( struct Node** head3,
struct Node* temp1,
struct Node* temp2)
{
while (temp1 != NULL) {
int val = temp1->data;
struct Node* t = temp2;
int x = 0;
while (t != NULL) {
if (t->data == val) {
x = 1;
break ;
}
t = t->next;
}
if (x == 0) {
push(head3, temp1->data);
}
temp1 = temp1->next;
}
}
void removeCommonNodes( struct Node* head1,
struct Node* head2)
{
struct Node* head3 = NULL;
traverse(&head3, head1, head2);
traverse(&head3, head2, head1);
printList(head3);
}
int main()
{
struct Node* head1 = NULL;
push(&head1, 20);
push(&head1, 5);
push(&head1, 15);
push(&head1, 10);
struct Node* head2 = NULL;
push(&head2, 10);
push(&head2, 20);
push(&head2, 15);
push(&head2, 8);
removeCommonNodes(head1, head2);
return 0;
}
|
Java
class GFG{
static class Node {
int data;
Node next;
};
static void printList(Node p)
{
if (p == null ) {
System.out.print( "[]" );
}
while (p != null ) {
System.out.print(p.data+ "->" );
p = p.next;
}
}
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;
}
static Node traverse(Node head3,
Node temp1,
Node temp2)
{
while (temp1 != null ) {
int val = temp1.data;
Node t = temp2;
int x = 0 ;
while (t != null ) {
if (t.data == val) {
x = 1 ;
break ;
}
t = t.next;
}
if (x == 0 ) {
head3 = push(head3, temp1.data);
}
temp1 = temp1.next;
}
return head3;
}
static void removeCommonNodes(Node head1,
Node head2)
{
Node head3 = null ;
head3 = traverse(head3, head1, head2);
head3 = traverse(head3, head2, head1);
printList(head3);
}
public static void main(String[] args)
{
Node head1 = new Node();
head1 = push(head1, 20 );
head1 = push(head1, 5 );
head1 = push(head1, 15 );
head1 = push(head1, 10 );
Node head2 = new Node();
head2 = push(head2, 10 );
head2 = push(head2, 20 );
head2 = push(head2, 15 );
head2 = push(head2, 8 );
removeCommonNodes(head1, head2);
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
def printList(p):
if (p = = None ):
print ( '[]' , end = '')
while (p ! = None ):
print (p.data, end = ' -> ' )
p = p. next
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
def traverse(head3, temp1, temp2):
while (temp1 ! = None ):
val = temp1.data
t = temp2
x = 0
while (t ! = None ):
if (t.data = = val):
x = 1
break
t = t. next
if (x = = 0 ):
head3 = push(head3, temp1.data)
temp1 = temp1. next
return head3
def removeCommonNodes(head1, head2):
head3 = None
head3 = traverse(head3, head1, head2)
head3 = traverse(head3, head2, head1)
printList(head3)
if __name__ = = '__main__' :
head1 = None
head1 = push(head1, 20 )
head1 = push(head1, 5 )
head1 = push(head1, 15 )
head1 = push(head1, 10 )
head2 = None
head2 = push(head2, 10 )
head2 = push(head2, 20 )
head2 = push(head2, 15 )
head2 = push(head2, 8 )
removeCommonNodes(head1, head2)
|
C#
using System;
class GFG{
class Node {
public int data;
public Node next;
};
static void printList(Node p)
{
if (p == null ) {
Console.Write( "[]" );
}
while (p != null ) {
Console.Write(p.data+ "->" );
p = p.next;
}
}
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;
}
static Node traverse(Node head3,
Node temp1,
Node temp2)
{
while (temp1 != null ) {
int val = temp1.data;
Node t = temp2;
int x = 0;
while (t != null ) {
if (t.data == val) {
x = 1;
break ;
}
t = t.next;
}
if (x == 0) {
head3 = push(head3, temp1.data);
}
temp1 = temp1.next;
}
return head3;
}
static void removeCommonNodes(Node head1,
Node head2)
{
Node head3 = null ;
head3 = traverse(head3, head1, head2);
head3 = traverse(head3, head2, head1);
printList(head3);
}
public static void Main(String[] args)
{
Node head1 = new Node();
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
Node head2 = new Node();
head2 = push(head2, 10);
head2 = push(head2, 20);
head2 = push(head2, 15);
head2 = push(head2, 8);
removeCommonNodes(head1, head2);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function printList(p) {
if (p == null ) {
document.write( "" );
}
while (p != null ) {
document.write(p.data + "->" );
p = p.next;
}
}
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 traverse(head3, temp1, temp2) {
while (temp1 != null ) {
var val = temp1.data;
var t = temp2;
var x = 0;
while (t != null ) {
if (t.data == val) {
x = 1;
break ;
}
t = t.next;
}
if (x == 0) {
head3 = push(head3, temp1.data);
}
temp1 = temp1.next;
}
return head3;
}
function removeCommonNodes(head1, head2) {
var head3 = null ;
head3 = traverse(head3, head1, head2);
head3 = traverse(head3, head2, head1);
printList(head3);
}
var head1 = new Node();
head1 = push(head1, 20);
head1 = push(head1, 5);
head1 = push(head1, 15);
head1 = push(head1, 10);
var head2 = new Node();
head2 = push(head2, 10);
head2 = push(head2, 20);
head2 = push(head2, 15);
head2 = push(head2, 8);
removeCommonNodes(head1, head2);
</script>
|
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...