237. Delete Node in a Linked Lis

2018-06-20  本文已影响0人  April63

给一个结点删除它,但是不知道它的前任结点
因此有一个办法就是将该节点的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
        
上一篇下一篇

猜你喜欢

热点阅读