美文网首页
237. Delete Node in a Linked Lis

237. Delete Node in a Linked Lis

作者: April63 | 来源:发表于2018-06-20 16:33 被阅读0次

给一个结点删除它,但是不知道它的前任结点
因此有一个办法就是将该节点的value和下一个节点的value交换,然后删除下一个结点

# 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.val, node.val
        node.next = node.next.next

这样其实是麻烦了,就把下一个节点的值赋值过来,然后把下一个结点删掉就好了呀,笨死了

# 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. Delete Node in a Linked Lis

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