Skip to content

Instantly share code, notes, and snippets.

@lienista
Last active August 28, 2018 18:54
Show Gist options
  • Select an option

  • Save lienista/7461894e9d1cf4bac3491b2b01174434 to your computer and use it in GitHub Desktop.

Select an option

Save lienista/7461894e9d1cf4bac3491b2b01174434 to your computer and use it in GitHub Desktop.
(Algorithms in Javascript) CTCI 2.3 - Delete Middle Node: Implement an algorithm to delete a node in the middle (i.e., any node but the first and last node, not necessarily the exact middle) of a singly linked list, given only access to that node.
const removeElements = (head, val) => {
if(head === null) return head;
let current = head;
while(current.next !== null && current.val === val) {
current = current.next;
}
if(!current.next && current.val === val) return [];
head = current;
while(current.next !== null) {
if(current.next.val === val) {
current.next = current.next.next;
} else {
current = current.next;
}
}
return head;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment