Last active
August 28, 2018 18:54
-
-
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.
This file contains hidden or 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
| 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