Leetcode 链表の简单题训练集合(237 404 876) python

版权声明:本文章为原创,未经许可不得转载 https://blog.csdn.net/weixin_41864878/article/details/91040181

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  

猜你喜欢

转载自blog.csdn.net/weixin_41864878/article/details/91040181
今日推荐