请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
现有一个链表 – head = [4,5,1,9],它可以表示为:
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.
说明:
- 链表至少包含两个节点。
- 链表中所有节点的值都是唯一的。
- 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
- 不要从你的函数中返回任何结果。
解题思路
我们删除一个节点会碰到什么问题?下面删除3
1 -> 2 -> 3 -> 4
但是我们没有2
的指针,该怎么办呢?我们可以将3=3.next
1 -> 2 -> 4 -> 4
del
这样我们通过删除4
实现我们的操作。
class Solution:
def deleteNode(self, node):
"""
:type node: ListNode
:rtype: void Do not return anything, modify node in-place instead.
"""
if node == None:
return node
if node.next == None:
node = None
node.val = node.next.val
node.next = node.next.next
我将该问题的其他语言版本添加到了我的GitHub Leetcode
如有问题,希望大家指出!!!