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().
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>
|
Output:
Linked 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)
Recursive Approach :
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), 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:
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), 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:
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(Number of Nodes)
Space Complexity: O(Number of Nodes)
Please refer complete article on Remove duplicates from a sorted linked list for more details!