Javascript Program For Removing All Occurrences Of Duplicates From A Sorted Link...
source link: https://www.geeksforgeeks.org/javascript-program-for-removing-all-occurrences-of-duplicates-from-a-sorted-linked-list/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Javascript Program For Removing All Occurrences Of Duplicates From A Sorted Linked List
- Last Updated : 10 Jan, 2022
Given a sorted linked list, delete all nodes that have duplicate numbers (all occurrences), leaving only numbers that appear once in the original list.
Examples:
Input: 23->28->28->35->49->49->53->53 Output: 23->35 Input: 11->11->11->11->75->75 Output: empty List
Note that this is different from Remove Duplicates From Linked List
The idea is to maintain a pointer (prev) to the node which just previous to the block of nodes we are checking for duplicates. In the first example, the pointer prev would point to 23 while we check for duplicates for node 28. Once we reach the last duplicate node with value 28 (name it current pointer), we can make the next field of prev node to be the next of current and update current=current.next. This would delete the block of nodes with value 28 which has duplicates.
- Javascript
<script> // Javascript program to remove all // occurrences of duplicates from a // sorted linked list // Class to create Linked lIst // head of linked list var head = null ; class Node{ constructor(val) { // Default value of the next // pointer field this .val = val; this .next = null ; } } // Function to insert data nodes into // the Linked List at the front function insert(data) { var new_node = new Node(data); new_node.next = head; head = new_node; } // Function to remove all occurrences // of duplicate elements function removeAllDuplicates() { // Create a dummy node that acts like // a fake head of list pointing to the // original head var dummy = new Node(0); // Dummy node points to the original head dummy.next = head; var prev = dummy; var current = head; while (current != null ) { // Until the current and previous values // are same, keep updating current while (current.next != null && prev.next.val == current.next.val) current = current.next; // If current has unique value i.e current // is not updated, Move the prev pointer // to next node if (prev.next == current) prev = prev.next; // When current is updated to the last // duplicate value of that segment, make // prev the next of current else prev.next = current.next; current = current.next; } // Update original head to the next of // dummy node head = dummy.next; } // Function to print the list elements function printList() { var trav = head; if (head == null ) document.write( " List is empty" ); while (trav != null ) { document.write(trav.val + " " ); trav = trav.next; } } // Driver code insert(53); insert(53); insert(49); insert(49); insert(35); insert(28); insert(28); insert(23); document.write( "Before removal of duplicates<br/>" ); printList(); removeAllDuplicates(); document.write( "<br/>After removal of duplicates<br/>" ); printList(); // This code is contributed by umadevi9616 </script> |
Output:
List before removal of duplicates 23 28 28 35 49 49 53 53 List after removal of duplicates 23 35
Time Complexity: O(n)
Please refer complete article on Remove all occurrences of duplicates from a sorted Linked List for more details!
Recommend
-
28
I had an array with some duplicates. I wanted to remove them. I know how to do this, but I searched for solutions anyway to make sure I wasn’t missing some trick. The results were disappointing, very language specific, an...
-
15
I was making a list of common interview questions for junior front-end developers. One common question that pops up quite often is how to remove duplicates from a JavaScript array. Let's say we have to the followin...
-
7
Leet Code - Remove Duplicates from Sorted Array **`Given an integer array nu...
-
5
(Remove Duplicates from Sorted Array) 25 Aug 2012
-
7
(Remove Duplicates from Sorted List) 25 Aug 2012
-
3
Isaac DH 16 hours ago Removing Duplicates from several columns in Crystal Reports 32 Views
-
6
Python Program For Removing All Occurrences Of Duplicates From A Sorted Linked ListLast Updated : 19 May, 2022Given a sorted linked list, delete all nodes that have duplic...
-
3
Python program to Count occurrences of each word in given text fileSkip to content ...
-
5
JavaScript Program to Find Index of First Occurrence of Target Element in Sorted Array...
-
5
Javascript Program For Removing Duplicates From An Unsorted Linked List ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK