Skip to content

Instantly share code, notes, and snippets.

@rayjcwu
Created February 24, 2014 23:05
Show Gist options
  • Select an option

  • Save rayjcwu/9199162 to your computer and use it in GitHub Desktop.

Select an option

Save rayjcwu/9199162 to your computer and use it in GitHub Desktop.
public Node removeNode(Node head, int val) {
Node fakeHead = new Node(0);
Node tail = fakeHead;
while (head != null) {
if (head.val != val) {
tail.next = head;
tail = tail.next;
}
head = head.next;
}
tail.next = null;
return fakeHead.next;
}
public NOde removeNode(Node node, int val) {
if (node == null) {
return null;
} else if (node.val == val) {
return removeNode(node.next, val);
} else {
node.next = removeNode(node.next, val);
return node;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment