#237. Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4
and you are given the third node with value 3
, the linked list should become 1 -> 2 -> 4
after calling your function.
第一眼看到竟然懵逼了,纳闷为啥不传总链表的引用,这怎么改变指针路线…… 一万个XXX奔跑。真的蠢... 其实只需要把当前链表变成下一个链表就好了,克隆下一个链表来代替当前链表。
Java Solution
public class Solution {
public void deleteNode(ListNode node) {
//因为已经说过这个节点不可能是尾节点
node.val = node.next.val;
node.next = node.next.next;
}
}