思路:因为我们无法访问我们想要删除的节点之前的节点,我们始终不能修改该节点的 next
指针。相反,我们必须分两步进行:
1.将想要删除的节点的值替换为它后面节点中的值,
2.然后删除它之后的节点。
# 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.
"""
node.val=node.next.val
node.next=node.next.next