-
-
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
This file contains 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
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