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?