Description
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.
很简单的一个题目:
难点就是遍历到这个node的时候,是不知道他的前一个节点信息的。所以,我们可以把要删除的node节点的next删掉,并把它的信息(val以及next)copy到node节点即可。
public void deleteNode(ListNode node) {
node.val = node.next.val;
node.next = node.next.next;
}