Python实现"删除链表中的节点(Delete Node in a Linked List)"的一种方法

写一个函数实现删除链表中的节点,给定的输入数据只有待删除的节点

给定链表--head = [4,5,1,9],形式为

 4 -> 5 -> 1 -> 9

Example 1:

Input: head = [4,5,1,9], node = 5
Output: [4,1,9]
Explanation: You are given the second node with value 5, the linked list
             should become 4 -> 1 -> 9 after calling your function.

Example 2:

Input: head = [4,5,1,9], node = 1
Output: [4,5,9]
Explanation: You are given the third node with value 1, the linked list
             should become 4 -> 5 -> 9 after calling your function.

注意:

链表至少有两个节点

所有节点的值均唯一,不重复

给定的节点不是尾节点,并且在链表中总是有效

函数不需要返回任何东西

1:node.val = node.next.val、node.next = node.next.next

def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        node.val = node.next.val
        node.next = node.next.next

算法题来自:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/description/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值