Leetcode 鏈表の簡單題訓練集合(237 404 876) python

237.刪除鏈表中的節點

在這裏插入圖片描述
這裏的node就是鏈表本身的結構,只用把下一個節點的值和指針賦值給node就行,題目出得比較巧妙吧

class Solution(object):
    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

707.設計鏈表

設計鏈表的實現。您可以選擇使用單鏈表或雙鏈表。單鏈表中的節點應該具有兩個屬性:val 和 next。val 是當前節點的值,next 是指向下一個節點的指針/引用。如果要使用雙向鏈表,則還需要一個屬性 prev 以指示鏈表中的上一個節點。假設鏈表中的所有節點都是 0-index 的。

在鏈表類中實現這些功能:

get(index):獲取鏈表中第 index 個節點的值。如果索引無效,則返回-1。
addAtHead(val):在鏈表的第一個元素之前添加一個值爲 val 的節點。插入後,新節點將成爲鏈表的第一個節點。
addAtTail(val):將值爲 val 的節點追加到鏈表的最後一個元素。 addAtIndex(index,val):在鏈表中的第
index 個節點之前添加值爲 val 的節點。如果 index 等於鏈表的長度,則該節點將附加到鏈表的末尾。如果 index
大於鏈表長度,則不會插入節點。 deleteAtIndex(index):如果索引 index 有效,則刪除鏈表中的第 index 個節點。

示例:

MyLinkedList linkedList = new MyLinkedList(); linkedList.addAtHead(1);
linkedList.addAtTail(3); linkedList.addAtIndex(1,2); //鏈表變爲1-> 2-> 3
linkedList.get(1); //返回2 linkedList.deleteAtIndex(1);
//現在鏈表是1-> 3 linkedList.get(1); //返回3

提示:

所有值都在 [1, 1000] 之內。 操作次數將在 [1, 1000] 之內。 請不要使用內置的 LinkedList 庫。

這個題真的很沙雕,首先python中完全可以只用列表實現,我的代碼中不寫self.head和self.tail都是ok的
其次,題目說了index從0開始,然而有一個測試用例index = -1的時候也成功插入了
就把人搞得很暴躁

class ListNode(object):
    def __init__(self, x):
        self.val = x
        self.next = None

class MyLinkedList(object):

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.head = ListNode(None)
        self.val_list = []
        self.tail = self.head

    def get(self, index):
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        :type index: int
        :rtype: int
        """
        if index >= 0 and index <= len(self.val_list)-1: #這裏一定要做index爲正值的限定!
            return self.val_list[index]
        else:
            return -1
        
    def addAtHead(self, val):
        """
        Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
        :type val: int
        :rtype: None
        """
        head = ListNode(val)
        head.next = self.head
        self.head = head
        self.val_list.insert(0, val)

    def addAtTail(self, val):
        """
        Append a node of value val to the last element of the linked list.
        :type val: int
        :rtype: None
        """
        tail = ListNode(val)
        self.tail.next = tail
        self.tail = tail
        self.val_list.append(val)
        
    def addAtIndex(self, index, val):
        """
        Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
        :type index: int
        :type val: int
        :rtype: None
        """
        if index == len(self.val_list): self.addAtTail(val)
        elif index == -1: self.addAtHead(val) #沙雕的index = -1時的插入
        elif index < len(self.val_list) and index >= 0: #必須做index爲正值的限定
            node = self.head
            steps = 0
            while index - 1 > steps:
                node = node.next
                steps += 1
            tmp = node.next
            new = ListNode(val)
            node.next = new
            new.next = tmp
            self.val_list.insert(index, val)

    def deleteAtIndex(self, index):
        """
        Delete the index-th node in the linked list, if the index is valid.
        :type index: int
        :rtype: None
        """
        if index < len(self.val_list) and index >= 0:
            node = self.head
            steps = 0
            while index-1 > steps:
                node = node.next
                steps += 1
            rem = node.next
            node.next = rem.next
            self.val_list.pop(index)

# Your MyLinkedList object will be instantiated and called as such:
# obj = MyLinkedList()
# param_1 = obj.get(index)
# obj.addAtHead(val)
# obj.addAtTail(val)
# obj.addAtIndex(index,val)
# obj.deleteAtIndex(index)

876.鏈表的中間結點

給定一個帶有頭結點 head 的非空單鏈表,返回鏈表的中間結點。

如果有兩個中間結點,則返回第二個中間結點。

示例 1:

輸入:[1,2,3,4,5] 輸出:此列表中的結點 3 (序列化形式:[3,4,5]) 返回的結點值爲 3 。
(測評系統對該結點序列化表述是 [3,4,5])。 注意,我們返回了一個 ListNode 類型的對象 ans,這樣: ans.val =
3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next =
NULL. 示例 2:

輸入:[1,2,3,4,5,6] 輸出:此列表中的結點 4 (序列化形式:[4,5,6]) 由於該列表有兩個中間結點,值分別爲 3 和
4,我們返回第二個結點。

提示:

給定鏈表的結點數介於 1 和 100 之間。

直接用快慢指針

class Solution(object):
    def middleNode(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        slow = head
        fast = head.next
        while fast:
            slow = slow.next
            if fast.next:
                fast = fast.next.next
            else: 
                fast = fast.next
        return slow  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章