Remove duplicates from a sorted linked list using recursion
Last Updated :
12 Jan, 2023
Write a removeDuplicates() function which 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 recursively from the head (or start) to end and after completion of recursion calls, compare the next node(returned node) and current node(head). If data of both nodes are equal then return the next (head-> next) node else return the current node(head).
Implementation: Functions other than removeDuplicates() are just to create a linked list and test removeDuplicates().
C++
#include <bits/stdc++.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* removeDuplicates( struct Node* head)
{
if (head == NULL)
return NULL;
head->next = removeDuplicates(head->next);
if (head->next != NULL &&
head->next->data == head->data) {
Node* res = head->next;
delete head;
return res;
}
return head;
}
void push( struct Node** head_ref, int new_data)
{
struct Node* new_node = new 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 " );
printList(head);
struct Node* h = removeDuplicates(head);
printf ( "\n Linked list after duplicate removal " );
printList(h);
return 0;
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node removeDuplicates( Node head)
{
if (head == null )
return null ;
head.next = removeDuplicates(head.next);
if (head.next != null &&
head.next.data == head.data)
{
Node res = head.next;
return res;
}
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.printf( "%d " , 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.printf( "\n Linked list before duplicate removal " );
printList(head);
Node h = removeDuplicates(head);
System.out.printf( "\n Linked list after duplicate removal " );
printList(h);
}
}
|
Python
class Node:
def __init__( self , new_data):
self .data = new_data
self . next = None
def removeDuplicates( head) :
if (head = = None ) :
return None
head. next = removeDuplicates(head. next )
if (head. next ! = None and
head. next .data = = head.data):
res = head. next
return res
return head
def push( head_ref, new_data) :
new_node = Node( 0 )
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
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 ( "\n Linked list before duplicate removal " )
printList(head)
h = removeDuplicates(head)
print ( "\n Linked list after duplicate removal " )
printList(h)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node removeDuplicates( Node head)
{
if (head == null )
return null ;
head.next = removeDuplicates(head.next);
if (head.next != null &&
head.next.data == head.data)
{
Node res = head.next;
return res;
}
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( "{0} " , 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( "\n Linked list before duplicate removal " );
printList(head);
Node h = removeDuplicates(head);
Console.Write( "\n Linked list after duplicate removal " );
printList(h);
}
}
|
Javascript
<script>
class Node {
constructor() {
this .data = 0;
this .next = null ;
}
}
function removeDuplicates(head) {
if (head == null ) return null ;
head.next = removeDuplicates(head.next);
if (head.next != null && head.next.data == head.data) {
var res = head.next;
return res;
}
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 " );
printList(head);
var h = removeDuplicates(head);
document.write( "<br> Linked list after duplicate removal " );
printList(h);
</script>
|
Output:
Linked list before duplicate removal 11 11 11 13 13 20
Linked list after duplicate removal 11 13 20
Time Complexity: O(N), to traverse n nodes in the Linked List
Auxiliary Space: O(N), to store n nodes in the Linked List.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...