美文网首页
237. 删除链表中的节点(easy)

237. 删除链表中的节点(easy)

作者: genggejianyi | 来源:发表于2019-06-28 15:46 被阅读0次

    请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
    现有一个链表 -- head = [4,5,1,9],它可以表示为:



    示例 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.

    *show the code:

    # Definition for singly-linked list.
    # class ListNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    
    class Solution(object):
        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
    
    • 此题注意链表别断裂就行了,因为我们无法访问删除节点的上一个节点,所以修改一下指针以及当前节点的值即可。

    相关文章

      网友评论

          本文标题:237. 删除链表中的节点(easy)

          本文链接:https://www.haomeiwen.com/subject/vxcecctx.html