Skip to content

Instantly share code, notes, and snippets.

@jason51122
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jason51122/94802d43cf85c27ccd29 to your computer and use it in GitHub Desktop.
Save jason51122/94802d43cf85c27ccd29 to your computer and use it in GitHub Desktop.
2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node. EXAMPLE Input: the node c from the linked list a->b->c->d->e Result: nothing isreturned, but the new linked list looks like a- >b- >d->e
class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
next = null;
}
}
public void deleteNode(ListNode cur) {
// Can not delete cur when it is the last node
if (cur == null || cur.next == null) {
return;
}
cur.val = cur.next.val;
cur.next = cur.next.next;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment