Home > Web Front-end > JS Tutorial > Javascript program to remove duplicates from sorted linked list

Javascript program to remove duplicates from sorted linked list

PHPz
Release: 2023-09-15 23:41:02
forward
1060 people have browsed it

用于从排序链接列表中删除重复项的 Javascript 程序

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

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
Copy after login

Instructions - The given linked list is sorted which makes it easy to find the duplicate elements and we can remove them by skipping if they are equal to the previous value.

Let’s see how the code is implemented

method

We will follow the steps below to resolve the issue -

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

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

  • We will create a function to pass the head of the linked list from which duplicate elements are to be removed 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 1. In these cases we return the head as is.

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

  • If the values ​​of the current node and the next node are equal, then we will move the next node to the next node and update the address of the next node of the current node.

  • Otherwise, we move to the next node and move the next node to its next node.

  • Finally we will return the header and print the value present in it.

Example

Let us implement the given steps in 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)
Copy after login

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. The time complexity is linear because we only traverse the linked list once.

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

in conclusion

In this tutorial, we implemented a JavaScript program to remove duplicate elements from a given sorted linked list. Since the linked list is sorted, all duplicate elements are adjacent to each other and can be easily removed by traversing it. The time complexity of the program we implemented is O(N) and the space complexity is O(1).

The above is the detailed content of Javascript program to remove duplicates from sorted linked list. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template