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.
public void deleteNode(ListNode node) {
if(node == null || node.next == null) return;
node.val = node.next.val;
node.next = node.next.next;
}

本文介绍如何通过给定单链表中的非尾节点,实现将其值替换为下一个节点的值,并将该节点的指针指向下一个节点的下一个节点,从而删除指定节点。以示例链表1->2->3->4为例,给定值为3的节点后,链表变为1->2->4。
986

被折叠的 条评论
为什么被折叠?



