LeetCode(237): Delete Node in a Linked List

本文介绍了一种在链表中删除指定节点的方法,该方法仅需要访问待删除节点即可完成操作,无需直接获取链表头结点。通过将待删除节点的值替换为下一个节点的值,再删除下一个节点的方式实现目标节点的删除。此方法适用于除尾节点外的所有节点。

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.

题意:在链表中,删除一个给定的节点。

思路:题目并没有给出链表的头结点,本题使用将要删除节点的下一个节点的值赋予要删除的节点,然后将要删除的节点的洗衣歌节点删除,可以达到等价的效果。

代码:

public void deleteNode(ListNode node) {
        node.val = node.next.val;
        node.next = node.next.next;
    }

转载于:https://www.cnblogs.com/Lewisr/p/5128204.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值