leetcode--删除链表中的节点

发布时间:2020-07-16 11:54:51 作者:ading2016
来源:网络 阅读:208

请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

现有一个链表 -- head = [4,5,1,9],它可以表示为:

leetcode--删除链表中的节点

 

示例 1:

输入: head = [4,5,1,9], node = 5输出: [4,1,9]解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.

示例 2:

输入: head = [4,5,1,9], node = 1输出: [4,5,9]解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.

 

说明:


我的解题:

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def deleteNode(self, node):
        """
        :type node: ListNode
        :rtype: void Do not return anything, modify node in-place instead.
        """
        # 将指定节点的下一个节点值赋值给本节点,并将下下节点指向下节点。
        nextnode = node.next
        after_nexnode = node.next.next
        node.val = nextnode.val
        node.next = after_nexnode

执行用时 : 64 ms, 在Delete Node in a Linked List的Python3提交中击败了55.73% 的用户

内存消耗 : 13.5 MB, 在Delete Node in a Linked List的Python3提交中击败了81.89% 的用户


推荐阅读:
  1. 链表节点的删除(删除重复无序节点)
  2. 链表节点的删除(链表data升序有重复)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 算法 tc

上一篇:jsp的自定义标签是什么?

下一篇:解决python读入不定行字符的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》