C# Program For Removing Duplicates From A Sorted Linked List
Last Updated :
10 Jan, 2023
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#
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();
}
}
|
Output
List 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)
Recursive Approach :
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( "Linked list after" +
" duplicate removal " );
printList(head);
}
}
|
Output
Linked list before duplicate removal 11 11 11 13 13 20Linked 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), due to recursive stack where n is the number of nodes in the given linked list.
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#
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();
}
}
|
Output
List 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), no extra space is required, so it is a constant.
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#
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(
"Linked list after duplicate removal " );
removeDuplicates(head);
}
}
|
Output
Linked list before duplicate removal 11 11 11 13 13 20 Linked list after duplicate removal 11 13 20
Time Complexity: O(Number of Nodes)
Auxiliary Space: O(Number of Nodes)
Please refer complete article on Remove duplicates from a sorted linked list for more details!
Share your thoughts in the comments
Please Login to comment...