Javascript Program For Removing Duplicates From A Sorted Linked List


Linked list is linear data structure and we have given a sorted linked list that consists of the integers. There are some numbers that may be duplicated or repeated and we have to remove them. As the given linked list is sorted, we can simply iterate over it and by using the while loop can remove the duplicate nodes from it. We will implement a proper code to understand the logic better with the discussion of time and space complexity.

Example

Given linked list is: 1-> 2 -> 2 -> 3 -> 4 -> 4 -> 4 -> 5 -> 5 -> 5-> 6-> null
Output: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null

Explanation − The given linked list is in the sorted way which makes it easy to find the elements which are duplicates and we can remove them by just skipping if they are equal to the previous value.

Let us see the approach of the code

Approach

We are going to follow these steps to solve the problem −

  • First, we will create a class to provide the structure to the nodes of the linked list.

  • Secondly, we will create the functions to print the linked list and add a new node to the existing linked list.

  • We will create a function to pass the head of the linked list from which we want to remove the duplicate elements and it will return the head of the new linked list.

  • First, we will check if the linked list is empty or if its size is equal to one. In these cases, we will return the head as it is.

  • We will create two variables, one indicating to the head and another one to the next node of the head.

  • If the value of both current and the next node is equal then we will move the next node to its next and update the address of next node for current node.

  • Else, we will move to the next node and move the next node to its next.

  • At the end we will return the head and print the values present in it.

Example

Let us implement the given steps in the code for better understanding

// class to provide structure to linked list node
class Node{
   constructor(val){
      this.value = val
      this.next = null
   }
}
// function to print the linked list
function print(head){
   var temp = head;
   if(head == null){
      console.log("The given linked list is empty");
   } else {
      var ans = ""
      while(temp.next != null){
         ans += temp.value;
         ans += " -> "
         temp = temp.next
      }
      ans += temp.value
      ans += " -> null"
   }
   console.log(ans)
}
// function to add data in linked list 
function add(data, head, tail){
   var new_node = new Node(data);
   if(head == null){
      head = new_node
      return new_node
   } else {
      tail.next = new_node;
      return new_node
   }
}
// function to remove the duplicate numbers 
function removeDupli(head){
   // if linked list is empty 
   if(head == null){
      return head;
   }
   // if linked list is of size one 
   if(head.next == null){
      return head;
   }
   var temp = head
   var next = head.next
   while(next != null){
      if(temp.value == next.value){
         next = next.next;
         temp.next = next;
      } else {
         next = next.next;
         temp = temp.next;
      }
   }
   return head;
}
// defining linked list
var head  = new Node(1)
var tail  = head
tail = add(2,head, tail)
tail = add(2,head, tail)
tail = add(3,head, tail)
tail = add(4,head, tail)
tail = add(4,head, tail)
tail = add(4,head, tail)
tail = add(5,head, tail)
tail = add(5,head, tail)
tail = add(5,head, tail)
tail = add(6,head, tail)
console.log("The given linked list is: ")
print(head)
// calling function to remove duplicate elements 
head = removeDupli(head)
console.log("The Linked list after removal of duplicate integers is: ")
print(head)

Time and Space Complexity

The time complexity of the above code is O(N) where N is the total number of nodes in the given linked list. Time complexity is linear as we have traversed over the linked list only once.

The space complexity of the above code is O(1), as we are not using any extra space.

Conclusion

In this tutorial, we have implemented a JavaScript program to remove the duplicate elements from the given sorted linked list. As the linked list is sorted means all the duplicate element are present next to each other and can be removed easily by traversing over it. The time complexity of the program which we have implement is O(N) and the space complexity is O(1).

Updated on: 12-Apr-2023

228 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements