Write a function that takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.
Algorithm: Traverse the list from the head (or start) node. While traversing, compare each node with its next node. If the data of the next node is the same as the current node then delete the next node. Before we delete a node, we need to store the next pointer of the node
Implementation: Functions other than removeDuplicates() are just to create a linked list and test removeDuplicates().
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void removeDuplicates(Node* head)
{
Node* current = head;
Node* next_next;
if (current == NULL)
return ;
while (current->next != NULL) {
if (current->data == current->next->data) {
next_next = current->next->next;
free (current->next);
current->next = next_next;
}
else
{
current = current->next;
}
}
}
void 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;
}
void printList(Node* node)
{
while (node != NULL) {
cout << " " << node->data;
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
cout << "Linked list before duplicate removal " << endl;
printList(head);
removeDuplicates(head);
cout << "\nLinked list after duplicate removal "
<< endl;
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
void removeDuplicates( struct Node* head)
{
struct Node* current = head;
struct Node* next_next;
if (current == NULL)
return ;
while (current->next != NULL) {
if (current->data == current->next->data) {
next_next = current->next->next;
free (current->next);
current->next = next_next;
}
else
{
current = current->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 printList( struct Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
printf ( "\n Linked list before duplicate removal \n" );
printList(head);
removeDuplicates(head);
printf ( "\n Linked list after duplicate removal \n" );
printList(head);
return 0;
}
|
Java
import java.io.*;
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void removeDuplicates()
{
Node curr = head;
while (curr != null ) {
Node temp = curr;
while (temp != null && temp.data == curr.data) {
temp = temp.next;
}
curr.next = temp;
curr = curr.next;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push( 20 );
llist.push( 13 );
llist.push( 13 );
llist.push( 11 );
llist.push( 11 );
llist.push( 11 );
System.out.println(
"List before removal of duplicates" );
llist.printList();
llist.removeDuplicates();
System.out.println(
"List after removal of elements" );
llist.printList();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
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
def deleteNode( self , key):
temp = self .head
if (temp is not None ):
if (temp.data = = key):
self .head = temp. next
temp = None
return
while (temp is not None ):
if temp.data = = key:
break
prev = temp
temp = temp. next
if (temp = = None ):
return
prev. next = temp. next
temp = None
def printList( self ):
temp = self .head
while (temp):
print (temp.data, end = ' ' )
temp = temp. next
def removeDuplicates( self ):
temp = self .head
if temp is None :
return
while temp. next is not None :
if temp.data = = temp. next .data:
new = temp. next . next
temp. next = None
temp. next = new
else :
temp = temp. next
return self .head
llist = LinkedList()
llist.push( 20 )
llist.push( 13 )
llist.push( 13 )
llist.push( 11 )
llist.push( 11 )
llist.push( 11 )
print ( "Created Linked List: " )
llist.printList()
print ()
print ( "Linked List after removing" ,
"duplicate elements:" )
llist.removeDuplicates()
llist.printList()
|
C#
using System;
public class LinkedList {
Node head;
class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void removeDuplicates()
{
Node current = head;
Node next_next;
if (head == null )
return ;
while (current.next != null ) {
if (current.data == current.next.data) {
next_next = current.next.next;
current.next = null ;
current.next = next_next;
}
else
current = current.next;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine();
}
public static void Main(String[] args)
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(13);
llist.push(13);
llist.push(11);
llist.push(11);
llist.push(11);
Console.WriteLine(
"List before removal of duplicates" );
llist.printList();
llist.removeDuplicates();
Console.WriteLine( "List after removal of elements" );
llist.printList();
}
}
|
Javascript
<script>
class Node
{
constructor(d) {
this .data = d;
this .next = null ;
}
}
let head= new Node();
function removeDuplicates()
{
let curr = head;
while (curr != null ) {
let temp = curr;
while (temp!= null && temp.data==curr.data) {
temp = temp.next;
}
curr.next = temp;
curr = curr.next;
}
}
function push(new_data)
{
let new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function printList()
{
let temp = head;
while (temp != null && temp.data)
{
document.write(temp.data+ " " );
temp = temp.next;
}
document.write( "<br>" );
}
push(20)
push(13)
push(13)
push(11)
push(11)
push(11)
document.write( "List before removal of duplicates " );
printList();
removeDuplicates();
document.write( "List after removal of elements " );
printList();
</script>
|
OutputLinked list before duplicate removal
11 11 11 13 13 20
Linked list after duplicate removal
11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1) , as there is no extra space used.
Recursive Approach :
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
};
void removeDuplicates(Node* head)
{
Node* to_free;
if (head == NULL)
return ;
if (head->next != NULL) {
if (head->data == head->next->data) {
to_free = head->next;
head->next = head->next->next;
free (to_free);
removeDuplicates(head);
}
else
{
removeDuplicates(head->next);
}
}
}
void 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;
}
void printList(Node* node)
{
while (node != NULL) {
cout << " " << node->data;
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
cout << "Linked list before duplicate removal " ;
printList(head);
removeDuplicates(head);
cout << "\nLinked list after duplicate removal " ;
printList(head);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* 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 printList( struct Node* node)
{
while (node != NULL) {
printf ( "%d " , node->data);
node = node->next;
}
}
Node* deleteDuplicates(Node* head)
{
if (head == nullptr)
return nullptr;
if (head->next == nullptr)
return head;
if (head->data == head->next->data) {
Node* tmp;
tmp = head->next;
head->next = head->next->next;
free (tmp);
return deleteDuplicates(head);
}
else {
head->next = deleteDuplicates(head->next);
return head;
}
}
int main()
{
struct Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
printf ( "\n Linked list before duplicate removal " );
printList(head);
head = deleteDuplicates(head);
printf ( "\n Linked list after duplicate removal " );
printList(head);
return 0;
}
|
Java
import java.io.*;
class GFG {
static class Node {
int data;
Node next;
};
static Node removeDuplicates(Node head)
{
Node to_free;
if (head == null )
return null ;
if (head.next != null ) {
if (head.data == head.next.data) {
to_free = head.next;
head.next = head.next.next;
removeDuplicates(head);
}
else {
removeDuplicates(head.next);
}
}
return head;
}
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 void printList(Node node)
{
while (node != null ) {
System.out.print( " " + node.data);
node = node.next;
}
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 20 );
head = push(head, 13 );
head = push(head, 13 );
head = push(head, 11 );
head = push(head, 11 );
head = push(head, 11 );
System.out.println( "Linked list before"
+ " duplicate removal " );
printList(head);
head = removeDuplicates(head);
System.out.println( "\nLinked list after"
+ " duplicate removal " );
printList(head);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def removeDuplicates(head):
if (head = = None ):
return
if (head. next ! = None ):
if (head.data = = head. next .data):
to_free = head. next
head. next = head. next . next
removeDuplicates(head)
else :
removeDuplicates(head. next )
return head
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
if __name__ = = '__main__' :
head = None
head = push(head, 20 )
head = push(head, 13 )
head = push(head, 13 )
head = push(head, 11 )
head = push(head, 11 )
head = push(head, 11 )
print ( "Linked list before duplicate removal " ,
end = "")
printList(head)
removeDuplicates(head)
print ( "\nLinked list after duplicate removal " ,
end = "")
printList(head)
|
C#
using System;
class GFG {
public class Node {
public int data;
public Node next;
};
static Node removeDuplicates(Node head)
{
Node to_free;
if (head == null )
return null ;
if (head.next != null ) {
if (head.data == head.next.data) {
to_free = head.next;
head.next = head.next.next;
removeDuplicates(head);
}
else {
removeDuplicates(head.next);
}
}
return head;
}
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 void printList(Node node)
{
while (node != null ) {
Console.Write( " " + node.data);
node = node.next;
}
}
public static void Main(String[] args)
{
Node head = null ;
head = push(head, 20);
head = push(head, 13);
head = push(head, 13);
head = push(head, 11);
head = push(head, 11);
head = push(head, 11);
Console.Write( "Linked list before"
+ " duplicate removal " );
printList(head);
head = removeDuplicates(head);
Console.Write( "\nLinked list after"
+ " duplicate removal " );
printList(head);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function removeDuplicates(head) {
var to_free;
if (head == null )
return null ;
if (head.next != null ) {
if (head.data == head.next.data) {
to_free = head.next;
head.next = head.next.next;
removeDuplicates(head);
}
else {
removeDuplicates(head.next);
}
}
return head;
}
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 printList(node) {
while (node != null ) {
document.write( " " + node.data);
node = node.next;
}
}
var head = null ;
head = push(head, 20);
head = push(head, 13);
head = push(head, 13);
head = push(head, 11);
head = push(head, 11);
head = push(head, 11);
document.write( "Linked list before" +
" duplicate removal <br/>" );
printList(head);
head = removeDuplicates(head);
document.write( "<br/>Linked list after" +
" duplicate removal <br/>" );
printList(head);
</script>
|
OutputLinked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(n)
Another Approach: Create a pointer that will point towards the first occurrence of every element and another pointer temp which will iterate to every element and when the value of the previous pointer is not equal to the temp pointer, we will set the pointer of the previous pointer to the first occurrence of another node.
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node( int d)
{
data = d;
next = NULL;
}
};
Node* removeDuplicates(Node* head)
{
Node *temp = head, *prev = head;
while (temp != NULL) {
if (temp->data != prev->data) {
prev->next = temp;
prev = temp;
}
temp = temp->next;
}
if (prev != temp)
prev->next = NULL;
return head;
}
Node* push(Node* head, int new_data)
{
Node* new_node = new Node(new_data);
new_node->next = head;
head = new_node;
return head;
}
void printList(Node* head)
{
Node* temp = head;
while (temp != NULL) {
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
int main()
{
Node* llist = NULL;
llist = push(llist, 20);
llist = push(llist, 13);
llist = push(llist, 13);
llist = push(llist, 11);
llist = push(llist, 11);
llist = push(llist, 11);
cout << ( "List before removal of duplicates\n" );
printList(llist);
cout << ( "List after removal of elements\n" );
llist = removeDuplicates(llist);
printList(llist);
}
|
C
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
int data;
struct Node* next;
} Node;
Node* removeDuplicates(Node* head)
{
Node *temp = head, *prev = head;
while (temp != NULL) {
if (temp->data != prev->data) {
prev->next = temp;
prev = temp;
}
temp = temp->next;
}
if (prev != temp)
prev->next = NULL;
return head;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = (Node*) malloc ( sizeof (Node));
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* head)
{
Node* temp = head;
while (temp != NULL) {
printf ( "%d " , temp->data);
temp = temp->next;
}
printf ( "\n" );
}
int main()
{
Node* llist = NULL;
push(&llist, 20);
push(&llist, 13);
push(&llist, 13);
push(&llist, 11);
push(&llist, 11);
push(&llist, 11);
printf ( "List before removal of duplicates\n" );
printList(llist);
printf ( "List after removal of elements\n" );
llist = removeDuplicates(llist);
printList(llist);
}
|
Java
import java.io.*;
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
void removeDuplicates()
{
Node temp = head, prev = head;
while (temp != null ) {
if (temp.data != prev.data) {
prev.next = temp;
prev = temp;
}
temp = temp.next;
}
if (prev != temp)
prev.next = null ;
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push( 20 );
llist.push( 13 );
llist.push( 13 );
llist.push( 11 );
llist.push( 11 );
llist.push( 11 );
System.out.print( "List before " );
System.out.println( "removal of duplicates" );
llist.printList();
llist.removeDuplicates();
System.out.println(
"List after removal of elements" );
llist.printList();
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def removeDuplicates(head):
if (head = = None and
head. next = = None ):
return
current = head
while (current. next ):
if current.data = = current. next .data:
current. next = current. next . next
else :
current = current. next
return
def push(head_ref, new_data):
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
if __name__ = = '__main__' :
head = None
head = push(head, 20 )
head = push(head, 13 )
head = push(head, 13 )
head = push(head, 11 )
head = push(head, 11 )
head = push(head, 11 )
print ( "List before removal of "
"duplicates " , end = "")
printList(head)
removeDuplicates(head)
print ( "\nList after removal of "
"elements " , end = "")
printList(head)
|
C#
using System;
class LinkedList {
public Node head;
public class Node {
public int data;
public Node next;
public Node( int d)
{
data = d;
next = null ;
}
}
void removeDuplicates()
{
Node temp = head, prev = head;
while (temp != null ) {
if (temp.data != prev.data) {
prev.next = temp;
prev = temp;
}
temp = temp.next;
}
if (prev != temp) {
prev.next = null ;
}
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine();
}
public static void Main( string [] args)
{
LinkedList llist = new LinkedList();
llist.push(20);
llist.push(13);
llist.push(13);
llist.push(11);
llist.push(11);
llist.push(11);
Console.Write( "List before " );
Console.WriteLine( "removal of duplicates" );
llist.printList();
llist.removeDuplicates();
Console.WriteLine( "List after removal of elements" );
llist.printList();
}
}
|
Javascript
<script>
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function removeDuplicates() {
var temp = head, prev = head;
while (temp != null ) {
if (temp.data != prev.data) {
prev.next = temp;
prev = temp;
}
temp = temp.next;
}
if (prev != temp) {
prev.next = null ;
}
}
function push(new_data) {
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function printList() {
var temp = head;
while (temp != null ) {
document.write(temp.data + " " );
temp = temp.next;
}
document.write( "<br/>" );
}
push(20);
push(13);
push(13);
push(11);
push(11);
push(11);
document.write( "List before " );
document.write( "removal of duplicates<br/>" );
printList();
removeDuplicates();
document.write( "List after removal of elements<br/>" );
printList();
</script>
|
OutputList before removal of duplicates
11 11 11 13 13 20
List after removal of elements
11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1)
Another Approach: Using Maps
The idea is to push all the values in a map and printing its keys.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node()
{
data = 0;
next = NULL;
}
};
void 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;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
void removeDuplicates(Node* head)
{
unordered_map< int , bool > track;
Node* temp = head;
while (temp) {
if (track.find(temp->data) == track.end()) {
cout << temp->data << " " ;
}
track[temp->data] = true ;
temp = temp->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 20);
push(&head, 13);
push(&head, 13);
push(&head, 11);
push(&head, 11);
push(&head, 11);
cout << "Linked list before duplicate removal " ;
printList(head);
cout << "\nLinked list after duplicate removal " ;
removeDuplicates(head);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node next;
Node()
{
data = 0 ;
next = null ;
}
}
class GFG {
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 void printList(Node node)
{
while (node != null ) {
System.out.print(node.data + " " );
node = node.next;
}
}
static void removeDuplicates(Node head)
{
HashMap<Integer, Boolean> track = new HashMap<>();
Node temp = head;
while (temp != null ) {
if (!track.containsKey(temp.data)) {
System.out.print(temp.data + " " );
}
track.put(temp.data, true );
temp = temp.next;
}
}
public static void main(String[] args)
{
Node head = null ;
head = push(head, 20 );
head = push(head, 13 );
head = push(head, 13 );
head = push(head, 11 );
head = push(head, 11 );
head = push(head, 11 );
System.out.print(
"Linked list before duplicate removal " );
printList(head);
System.out.print(
"\nLinked list after duplicate removal " );
removeDuplicates(head);
}
}
|
Python3
class Node:
def __init__( self ):
self .data = 0
self . next = None
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 printList(node):
while (node ! = None ):
print (node.data, end = " " )
node = node. next
def removeDuplicates(head):
track = {}
temp = head
while (temp ! = None ):
if ( not temp.data in track):
print (temp.data, end = " " )
track[temp.data] = True
temp = temp. next
head = None
head = push(head, 20 )
head = push(head, 13 )
head = push(head, 13 )
head = push(head, 11 )
head = push(head, 11 )
head = push(head, 11 )
print ( "Linked list before duplicate removal " , end = " " )
printList(head)
print ( "\nLinked list after duplicate removal " , end = " " )
removeDuplicates(head)
|
C#
using System;
using System.Collections.Generic;
public class Node {
public int data;
public Node next;
public Node()
{
data = 0;
next = null ;
}
}
public class GFG {
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 void printList(Node node)
{
while (node != null ) {
Console.Write(node.data + " " );
node = node.next;
}
}
static void removeDuplicates(Node head)
{
Dictionary< int , bool > track
= new Dictionary< int , bool >();
Node temp = head;
while (temp != null ) {
if (!track.ContainsKey(temp.data)) {
Console.Write(temp.data + " " );
track.Add(temp.data, true );
}
temp = temp.next;
}
}
static public void Main()
{
Node head = null ;
head = push(head, 20);
head = push(head, 13);
head = push(head, 13);
head = push(head, 11);
head = push(head, 11);
head = push(head, 11);
Console.Write(
"Linked list before duplicate removal " );
printList(head);
Console.Write(
"\nLinked list after duplicate removal " );
removeDuplicates(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
}
function push(head_ref, new_data)
{
let new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
head_ref = new_node;
return head_ref;
}
function printList(node)
{
while (node != null )
{
document.write(node.data + " " );
node = node.next;
}
}
function removeDuplicates(head)
{
let track = new Map();
let temp = head;
while (temp != null )
{
if (!track.has(temp.data))
{
document.write(temp.data + " " );
}
track.set(temp.data, true );
temp = temp.next;
}
}
let head = null ;
head = push(head, 20);
head = push(head, 13);
head = push(head, 13);
head = push(head, 11);
head = push(head, 11);
head = push(head, 11);
document.write( "Linked list before duplicate removal " );
printList(head);
document.write( "<br>Linked list after duplicate removal " );
removeDuplicates(head);
</script>
|
OutputLinked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(n)
Using Set data structure:
We know that set data structure always stores the unique element.
so, the intuition comes from there-
Explanation:
The code then defines a class Solution which contains the deleteDuplicates function. The function takes a pointer to the head of the linked list as an argument. If the head is empty, it returns it. Otherwise, it initializes a set data structure to store unique values in the linked list. It also declares two pointers curr and prev to traverse the linked list and keep track of the previous node, respectively.
The code then enters a while loop that continues as long as curr is not NULL. In each iteration, it checks if the value of the current node already exists in the set. If it does, the code updates the next pointer of the previous node to skip the current node and remove it from the linked list. If the value does not exist in the set, it is inserted into the set and prev is updated to become the current node. After each iteration, curr is updated to point to the next node.
Finally, the code returns the head of the linked list.
The main function demonstrates how to use the deleteDuplicates function by creating a linked list with values 1, 2, 2,2, 3 ,3,3and calling the function to remove duplicates. It then prints the linked list after duplicates are removed.
Note: The code assumes that the linked list is sorted and the duplicates appear consecutively
Below is the implementation for above approach :
C++
#include <iostream>
#include <set>
struct ListNode {
int val;
ListNode*
next;
ListNode( int x)
{
this ->val = x;
this ->next = NULL;
}
};
class Solution {
public :
ListNode* deleteDuplicates(ListNode* head)
{
if (!head)
return head;
std::set< int > set;
ListNode* curr
= head;
ListNode* prev = NULL;
while (curr) {
if (set.count(curr->val)) {
prev->next = curr->next;
}
else {
set.insert(curr->val);
prev = curr;
}
curr = curr->next;
}
return head;
}
};
int main()
{
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(2);
head->next->next->next->next = new ListNode(3);
head->next->next->next->next->next = new ListNode(3);
head->next->next->next->next->next->next
= new ListNode(3);
ListNode* BeforePrinter = head;
std::cout
<< "Before removing duplicates linked list is:"
<< std::endl;
while (BeforePrinter) {
std::cout << BeforePrinter->val << " " ;
BeforePrinter = BeforePrinter->next;
}
std::cout << std::endl;
Solution solution;
head = solution.deleteDuplicates(head);
std::cout << "after removing duplicates linked list is:"
<< std::endl;
ListNode* curr = head;
while (curr) {
std::cout << curr->val << " " ;
curr = curr->next;
}
std::cout << std::endl;
return 0;
}
|
Python3
class ListNode:
def __init__( self , val = 0 , next = None ):
self .val = val
self . next = next
class Solution:
def deleteDuplicates( self , head: ListNode) - > ListNode:
if not head:
return head
unique_vals = set ()
curr = head
prev = None
while curr:
if curr.val in unique_vals:
prev. next = curr. next
else :
unique_vals.add(curr.val)
prev = curr
curr = curr. next
return head
head = ListNode( 1 )
head. next = ListNode( 2 )
head. next . next = ListNode( 2 )
head. next . next . next = ListNode( 2 )
head. next . next . next . next = ListNode( 3 )
head. next . next . next . next . next = ListNode( 3 )
head. next . next . next . next . next . next = ListNode( 3 )
BeforePrinter = head
print ( "Before removing duplicates linked list is:" )
while BeforePrinter:
print (BeforePrinter.val, end = " " )
BeforePrinter = BeforePrinter. next
print ()
solution = Solution()
head = solution.deleteDuplicates(head)
print ( "After removing duplicates linked list is:" )
curr = head
while curr:
print (curr.val, end = " " )
curr = curr. next
print ()
|
C#
using System;
using System.Collections.Generic;
class ListNode {
public int val;
public ListNode next;
public ListNode( int x)
{
this .val = x;
this .next = null ;
}
}
class Solution {
public ListNode DeleteDuplicates(ListNode head)
{
if (head == null )
return head;
HashSet< int > set = new HashSet< int >();
ListNode curr = head;
ListNode prev = null ;
while (curr != null ) {
if ( set .Contains(curr.val)) {
prev.next = curr.next;
}
else {
set .Add(curr.val);
prev = curr;
}
curr = curr.next;
}
return head;
}
}
class Program {
static void Main( string [] args)
{
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(2);
head.next.next.next.next = new ListNode(3);
head.next.next.next.next.next = new ListNode(3);
head.next.next.next.next.next.next
= new ListNode(3);
Console.WriteLine(
"Before removing duplicates linked list is:" );
ListNode beforePrinter = head;
while (beforePrinter != null ) {
Console.Write(beforePrinter.val + " " );
beforePrinter = beforePrinter.next;
}
Console.WriteLine();
Solution solution = new Solution();
head = solution.DeleteDuplicates(head);
Console.WriteLine(
"after removing duplicates linked list is:" );
ListNode curr = head;
while (curr != null ) {
Console.Write(curr.val + " " );
curr = curr.next;
}
Console.WriteLine();
}
}
|
Javascript
class Node {
constructor(x) {
this .val = x;
this .next = null ;
}
}
class Solution {
deleteDuplicates(head) {
if (head == null ) return head;
let set = new Set();
let curr = head;
let prev = null ;
while (curr) {
if (set.has(curr.val)) {
prev.next = curr.next;
} else {
set.add(curr.val);
prev = curr;
}
curr = curr.next;
}
return head;
}
};
let head = new Node(1);
head.next = new Node(2);
head.next.next = new Node(2);
head.next.next.next = new Node(2);
head.next.next.next.next = new Node(3);
head.next.next.next.next.next = new Node(3);
head.next.next.next.next.next.next = new Node(3);
let BeforePrinter = head;
console.log( "Before removing duplicates linked list is:" );
console.log( "\n" );
while (BeforePrinter) {
console.log(BeforePrinter.val + " " );
BeforePrinter = BeforePrinter.next;
}
console.log( "\n" );
let solution = new Solution();
head = solution.deleteDuplicates(head);
console.log( "after removing duplicates linked list is:" );
let curr = head;
console.log( "\n" );
while (curr) {
console.log(curr.val + " " );
curr = curr.next;
}
console.log( "\n" );
console.log();
|
Java
import java.util.*;
class ListNode {
int val;
ListNode
next;
ListNode( int x)
{
this .val = x;
this .next = null ;
}
}
class Solution {
public ListNode deleteDuplicates(ListNode head)
{
if (head == null )
return head;
Set<Integer> set = new HashSet<>();
ListNode curr
= head;
ListNode prev = null ;
while (curr != null ) {
if (set.contains(curr.val)) {
prev.next = curr.next;
}
else {
set.add(curr.val);
prev = curr;
}
curr = curr.next;
}
return head;
}
}
public class Main {
public static void main(String[] args)
{
ListNode head = new ListNode( 1 );
head.next = new ListNode( 2 );
head.next.next = new ListNode( 2 );
head.next.next.next = new ListNode( 2 );
head.next.next.next.next = new ListNode( 3 );
head.next.next.next.next.next = new ListNode( 3 );
head.next.next.next.next.next.next
= new ListNode( 3 );
ListNode BeforePrinter = head;
System.out.println(
"Before removing duplicates linked list is:" );
while (BeforePrinter != null ) {
System.out.print(BeforePrinter.val + " " );
BeforePrinter = BeforePrinter.next;
}
System.out.println();
Solution solution = new Solution();
head = solution.deleteDuplicates(head);
System.out.println(
"After removing duplicates linked list is:" );
ListNode curr = head;
while (curr != null ) {
System.out.print(curr.val + " " );
curr = curr.next;
}
System.out.println();
}
}
|
OutputBefore removing duplicates linked list is:
1 2 2 2 3 3 3
after removing duplicates linked list is:
1 2 3
Time Complexity: O(n)
Auxiliary Space: O(n)
Java
import java.util.HashSet;
class ListNode {
int val;
ListNode next;
ListNode( int x) { val = x; }
}
class Solution {
public ListNode deleteDuplicates(ListNode head)
{
if (head == null )
return head;
HashSet<Integer> set = new HashSet<>();
ListNode prev
= head;
ListNode curr = head.next;
set.add(head.val);
while (curr != null ) {
if (set.contains(curr.val)) {
prev.next = curr.next;
}
else {
set.add(curr.val);
prev = curr;
}
curr = curr.next;
}
return head;
}
}
public class Main {
public static void main(String[] args)
{
ListNode head = new ListNode( 1 );
head.next = new ListNode( 2 );
head.next.next = new ListNode( 2 );
head.next.next.next = new ListNode( 2 );
head.next.next.next.next = new ListNode( 3 );
head.next.next.next.next.next = new ListNode( 3 );
head.next.next.next.next.next.next
= new ListNode( 3 );
Solution solution = new Solution();
ListNode Beforeprinter = head;
System.out.print(
"before removing the duplicates linked list is:" );
while (Beforeprinter != null ) {
System.out.print(Beforeprinter.val + " " );
Beforeprinter = Beforeprinter.next;
}
System.out.println();
head = solution.deleteDuplicates(head);
System.out.print(
"after removing the duplicates linked list is:" );
ListNode curr = head;
while (curr != null ) {
System.out.print(curr.val + " " );
curr = curr.next;
}
System.out.println();
}
}
|
Python3
class ListNode:
def __init__( self , x):
self .val = x
self . next = None
class Solution:
def delete_duplicates( self , head: ListNode) - > ListNode:
if not head:
return head
s = set ()
prev = head
curr = head. next
s.add(head.val)
while curr:
if curr.val in s:
prev. next = curr. next
else :
s.add(curr.val)
prev = curr
curr = curr. next
return head
if __name__ = = '__main__' :
head = ListNode( 1 )
head. next = ListNode( 2 )
head. next . next = ListNode( 2 )
head. next . next . next = ListNode( 2 )
head. next . next . next . next = ListNode( 3 )
head. next . next . next . next . next = ListNode( 3 )
head. next . next . next . next . next . next = ListNode( 3 )
solution = Solution()
Beforeprinter = head
print ( "before removing the duplicates linked list is:" , end = " " )
while Beforeprinter:
print (Beforeprinter.val, end = " " )
Beforeprinter = Beforeprinter. next
print ()
head = solution.delete_duplicates(head)
print ( "after removing the duplicates linked list is:" , end = " " )
curr = head
while curr:
print (curr.val, end = " " )
curr = curr. next
print ()
|
Javascript
class ListNode {
constructor(val){
this .val = val;
this .next = null ;
}
}
class Solution {
deleteDuplicates(head) {
if (head == null ) return head;
let set = new Set();
let prev = head;
let curr = head.next;
set.add(head.val);
while (curr != null ) {
if (set.has(curr.val) == true ) {
prev.next = curr.next;
} else {
set.add(curr.val);
prev = curr;
}
curr = curr.next;
}
return head;
}
}
let head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(2);
head.next.next.next.next = new ListNode(3);
head.next.next.next.next.next = new ListNode(3);
head.next.next.next.next.next.next = new ListNode(3);
let solution = new Solution();
let Beforeprinter = head;
process.stdout.write( "before removing the duplicates linked list is: " );
while (Beforeprinter != null ) {
process.stdout.write(Beforeprinter.val + " " );
Beforeprinter = Beforeprinter.next;
}
process.stdout.write( "\n" );
head = solution.deleteDuplicates(head);
process.stdout.write( "after removing the duplicates linked list is: " );
let curr = head;
while (curr != null ) {
process.stdout.write(curr.val + " " );
curr = curr.next;
}
|
C++
#include <iostream>
#include <unordered_set>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode( int x)
: val(x)
, next(NULL)
{
}
};
class Solution {
public :
ListNode* deleteDuplicates(ListNode* head)
{
if (head == NULL)
return head;
unordered_set< int > set;
ListNode* prev
= head;
ListNode* curr = head->next;
set.insert(head->val);
while (curr != NULL) {
if (set.find(curr->val) != set.end()) {
prev->next = curr->next;
}
else {
set.insert(curr->val);
prev = curr;
}
curr = curr->next;
}
return head;
}
};
int main()
{
ListNode* head = new ListNode(1);
head->next = new ListNode(2);
head->next->next = new ListNode(2);
head->next->next->next = new ListNode(2);
head->next->next->next->next = new ListNode(3);
head->next->next->next->next->next = new ListNode(3);
head->next->next->next->next->next->next
= new ListNode(3);
Solution solution;
ListNode* Beforeprinter = head;
cout << "before removing the duplicates linked list "
"is: " ;
while (Beforeprinter != NULL) {
cout << Beforeprinter->val << " " ;
Beforeprinter = Beforeprinter->next;
}
cout << endl;
head = solution.deleteDuplicates(head);
cout
<< "after removing the duplicates linked list is: " ;
ListNode* curr = head;
while (curr != NULL) {
cout << curr->val << " " ;
curr = curr->next;
}
cout << endl;
return 0;
}
|
C#
using System;
using System.Collections.Generic;
public class ListNode {
public int val;
public ListNode next;
public ListNode( int val = 0, ListNode next = null )
{
this .val = val;
this .next = next;
}
}
public class Solution {
public ListNode DeleteDuplicates(ListNode head)
{
if (head == null ) {
return head;
}
HashSet< int > set = new HashSet< int >();
ListNode prev = head;
ListNode curr = head.next;
set .Add(head.val);
while (curr != null ) {
if ( set .Contains(curr.val)) {
prev.next = curr.next;
}
else {
set .Add(curr.val);
prev = curr;
}
curr = curr.next;
}
return head;
}
}
public class Program {
static void Main( string [] args)
{
ListNode head = new ListNode(1);
head.next = new ListNode(2);
head.next.next = new ListNode(2);
head.next.next.next = new ListNode(2);
head.next.next.next.next = new ListNode(3);
head.next.next.next.next.next = new ListNode(3);
head.next.next.next.next.next.next
= new ListNode(3);
Solution solution = new Solution();
ListNode beforePrinter = head;
Console.Write(
"Before removing the duplicates, linked list is: " );
while (beforePrinter != null ) {
Console.Write(beforePrinter.val + " " );
beforePrinter = beforePrinter.next;
}
Console.WriteLine();
head = solution.DeleteDuplicates(head);
Console.Write(
"After removing the duplicates, linked list is: " );
ListNode curr = head;
while (curr != null ) {
Console.Write(curr.val + " " );
curr = curr.next;
}
Console.WriteLine();
}
}
|
Outputbefore removing the duplicates linked list is:1 2 2 2 3 3 3
after removing the duplicates linked list is:1 2 3
Time complexity: O(n), where n is the number of nodes in the linked list. This is because we iterate through the linked list once, and each operation of checking the set and inserting into the set takes O(logn) time on average due to the underlying balanced binary search tree data structure.
Auxiliary Space: O(n), where n is the number of unique values in the linked list. This is because we use a set to store the unique values, which takes O(n) space.
This approach is contributed by Veerendra Singh Rajpoot
If you find anything wrong or incorrect in this approach please let me know.