237. 删除链表中的节点

exiaohu 于 2021-11-02 发布

题目链接:237. 删除链表中的节点

这个题目有点坑,中文翻译不知道要干什么。大概意思就是给出链表中指向要删除结点的指针,删除这个结点,给定的结点必不是链表最后一个结点。

这样就好理解了,删除给定结点的后继,然后把后继的值赋给给定结点,就能达到要删除的效果。

# 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