Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created October 6, 2021 23:44
Show Gist options
  • Save misterpoloy/688bf7186455bf3a67bf84702d75e5f1 to your computer and use it in GitHub Desktop.
Save misterpoloy/688bf7186455bf3a67bf84702d75e5f1 to your computer and use it in GitHub Desktop.
Remove duplciates from a sorted linked list
// 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;
@misterpoloy
Copy link
Author

Uploading screenshot.png…

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment