237. Delete Node in a Linked List
因为只能从指定需删除的node
访问,无法把前一个next
拉到下一个node
上去,只能通过改node
本身值的方式实现in-place
操作。
# 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.
"""
cur = node
while cur.next.next:
cur.val = cur.next.val
cur = cur.next
cur.val = cur.next.val
cur.next = None
Last updated
Was this helpful?