Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created July 20, 2020 07:05
Show Gist options
  • Save RP-3/5baa031e5b2402a6f4b829cfebd3a9a7 to your computer and use it in GitHub Desktop.
Save RP-3/5baa031e5b2402a6f4b829cfebd3a9a7 to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var removeElements = function(head, val) {
const dummy = { next: head };
let [prev, curr] = [dummy, head];
while(curr){
if(curr.val === val) prev.next = curr.next;
else prev = curr;
curr = curr.next;
}
return dummy.next;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment