Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list.
Examples:
Input : 23->28->28->35->49->49->53->53
Output : 23->35
Input : 11->11->11->11->75->75
Output : empty List
Note that this is different from Remove Duplicates From Linked List
The idea is to maintain a pointer (prev) to the node which just previous to the block of nodes we are checking for duplicates. In the first example, the pointer prev would point to 23 while we check for duplicates for node 28. Once we reach the last duplicate node with value 28 (name it current pointer), we can make the next field of prev node to be the next of current and update current=current.next. This would delete the block of nodes with value 28 which has duplicates.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
struct Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
void printList( struct Node* node)
{
while (node != NULL) {
cout<<node->data<< " " ;
node = node->next;
}
}
void removeAllDuplicates( struct Node*& start)
{
Node* dummy = new Node;
dummy->next = start;
Node* prev = dummy;
Node* current = start;
while (current != NULL) {
while (current->next != NULL && prev->next->data == current->next->data)
current = current->next;
if (prev->next == current)
prev = prev->next;
else
prev->next = current->next;
current = current->next;
}
start = dummy->next;
}
int main()
{
struct Node* start = newNode(23);
start->next = newNode(28);
start->next->next = newNode(28);
start->next->next->next = newNode(35);
start->next->next->next->next = newNode(49);
start->next->next->next->next->next = newNode(49);
start->next->next->next->next->next->next = newNode(53);
start->next->next->next->next->next->next->next = newNode(53);
cout << "List before removal of duplicates\n" ;
printList(start);
removeAllDuplicates(start);
cout << "\nList after removal of duplicates\n" ;
printList(start);
return 0;
}
|
C
#include<stdio.h>
#include<stdlib.h>
typedef struct Node {
int data;
struct Node* next;
}Node;
struct Node* newNode( int data)
{
Node* temp = (Node *) malloc ( sizeof (Node));
temp->data = data;
temp->next = NULL;
return temp;
}
void printList(Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
}
void removeAllDuplicates(Node* start)
{
Node* dummy = (Node *) malloc ( sizeof (Node));
dummy->next = start;
Node* prev = dummy;
Node* current = start;
while (current != NULL) {
while (current->next != NULL && prev->next->data == current->next->data)
current = current->next;
if (prev->next == current)
prev = prev->next;
else
prev->next = current->next;
current = current->next;
}
start = dummy->next;
}
int main()
{
struct Node* start = newNode(23);
start->next = newNode(28);
start->next->next = newNode(28);
start->next->next->next = newNode(35);
start->next->next->next->next = newNode(49);
start->next->next->next->next->next = newNode(49);
start->next->next->next->next->next->next = newNode(53);
start->next->next->next->next->next->next->next = newNode(53);
printf ( "List before removal of duplicates\n" );
printList(start);
removeAllDuplicates(start);
printf ( "\nList after removal of duplicates\n" );
printList(start);
return 0;
}
|
Java
class LinkedList{
Node head = null ;
class Node
{
int val;
Node next;
Node( int v)
{
val = v;
next = null ;
}
}
public void insert( int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
public void removeAllDuplicates()
{
Node dummy = new Node( 0 );
dummy.next = head;
Node prev = dummy;
Node current = head;
while (current != null )
{
while (current.next != null &&
prev.next.val == current.next.val)
current = current.next;
if (prev.next == current)
prev = prev.next;
else
prev.next = current.next;
current = current.next;
}
head = dummy.next;
}
public void printList()
{
Node trav = head;
if (head == null )
System.out.print( " List is empty" );
while (trav != null )
{
System.out.print(trav.val + " " );
trav = trav.next;
}
}
public static void main(String[] args)
{
LinkedList ll = new LinkedList();
ll.insert( 53 );
ll.insert( 53 );
ll.insert( 49 );
ll.insert( 49 );
ll.insert( 35 );
ll.insert( 28 );
ll.insert( 28 );
ll.insert( 23 );
System.out.println( "Before removal of duplicates" );
ll.printList();
ll.removeAllDuplicates();
System.out.println( "\nAfter removal of duplicates" );
ll.printList();
}
}
|
Python3
class Node:
def __init__( self , val):
self .val = val
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def push( self , new_data):
new_node = Node(new_data)
new_node. next = self .head
self .head = new_node
return new_node
def removeAllDuplicates( self , temp):
curr = temp
head = prev = Node( None )
head. next = curr
while curr and curr. next :
if curr.val = = curr. next .val:
while (curr and curr. next and
curr.val = = curr. next .val):
curr = curr. next
curr = curr. next
prev. next = curr
else :
prev = prev. next
curr = curr. next
return head. next
def printList( self ):
temp1 = self .head
while temp1 is not None :
print (temp1.val, end = " " )
temp1 = temp1. next
def get_head( self ):
return self .head
if __name__ = = '__main__' :
llist = LinkedList()
llist.push( 53 )
llist.push( 53 )
llist.push( 49 )
llist.push( 49 )
llist.push( 35 )
llist.push( 28 )
llist.push( 28 )
llist.push( 23 )
print ( 'Created linked list is:' )
llist.printList()
print ( '\nLinked list after deletion of duplicates:' )
head1 = llist.get_head()
llist.removeAllDuplicates(head1)
llist.printList()
|
C#
using System;
public class LinkedList
{
Node head = null ;
class Node
{
public int val;
public Node next;
public Node( int v)
{
val = v;
next = null ;
}
}
public void insert( int data)
{
Node new_node = new Node(data);
new_node.next = head;
head = new_node;
}
public void removeAllDuplicates()
{
Node dummy = new Node(0);
dummy.next = head;
Node prev = dummy;
Node current = head;
while (current != null )
{
while (current.next != null &&
prev.next.val == current.next.val)
current = current.next;
if (prev.next == current)
prev = prev.next;
else
prev.next = current.next;
current = current.next;
}
head = dummy.next;
}
public void printList()
{
Node trav = head;
if (head == null )
Console.Write( " List is empty" );
while (trav != null )
{
Console.Write(trav.val + " " );
trav = trav.next;
}
}
public static void Main(String[] args)
{
LinkedList ll = new LinkedList();
ll.insert(53);
ll.insert(53);
ll.insert(49);
ll.insert(49);
ll.insert(35);
ll.insert(28);
ll.insert(28);
ll.insert(23);
Console.WriteLine( "Before removal of duplicates" );
ll.printList();
ll.removeAllDuplicates();
Console.WriteLine( "\nAfter removal of duplicates" );
ll.printList();
}
}
|
Javascript
class Node {
constructor(val, next = null ) {
this .data = val;
this .next = next;
}
}
const node1 = new Node(11);
const node2 = new Node(11);
const node3 = new Node(11);
const node4 = new Node(75);
const node5 = new Node(75);
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = node5;
const removeDuplicate = (head) => {
let current = head;
let prev = null ;
while (current) {
if (current.next && current.data == current.next.data) {
let pivot = current;
let newCurrent = current.next
while (newCurrent && pivot.data === newCurrent.data) {
pivot.next = newCurrent.next;
newCurrent = newCurrent.next;
}
if (prev) prev.next = pivot.next;
else head = pivot.next
current = pivot.next
} else {
prev = current;
current = current.next;
}
}
return head;
}
console.log(JSON.stringify(removeDuplicate(node1)));
|
Output
List before removal of duplicates
23 28 28 35 49 49 53 53
List after removal of duplicates
23 35
Time Complexity: O(n)
Auxiliary Space: O(1) because extra space is not required in removal of duplicates
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
10 Jan, 2023
Like Article
Save Article