Created
October 6, 2021 23:44
-
-
Save misterpoloy/688bf7186455bf3a67bf84702d75e5f1 to your computer and use it in GitHub Desktop.
Remove duplciates from a sorted linked list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is an input class. Do not edit. | |
class LinkedList { | |
constructor(value) { | |
this.value = value; | |
this.next = null; | |
} | |
} | |
// O(n) time | O(1) space | |
function removeDuplicatesFromLinkedList(linkedList) { | |
let currentNode = linkedList | |
while (currentNode !== null) { | |
let nextNode = currentNode.next | |
while (nextNode !== null && currentNode.value == nextNode.value) { | |
nextNode = nextNode.next | |
} | |
currentNode.next = nextNode | |
currentNode = nextNode | |
} | |
return linkedList; | |
} | |
// Do not edit the lines below. | |
exports.LinkedList = LinkedList; | |
exports.removeDuplicatesFromLinkedList = removeDuplicatesFromLinkedList; |
Author
misterpoloy
commented
Oct 6, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment