1. 程式人生 > >LeetCode 237 Delete Node in a Linked List 解題報告

LeetCode 237 Delete Node in a Linked List 解題報告

oid none __init__ etc 刪除結點 下一個 cti type UNC

題目要求

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

題目分析及思路

要求寫一個函數,刪除一個單鏈表中的一個結點(除了最後一個)。函數不需要返回值,只需要原地修改給定刪除結點。我們可以將要刪除結點的兩個屬性用該結點的下一個結點的兩個屬性來替代。

python代碼

# 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

LeetCode 237 Delete Node in a Linked List 解題報告