leetcode 707 python实现

使用了结点的方式实现链表,速度比较慢,之后尝试了列表实现,速度快了很多

class MyLinkedList:
    # 定义一个结点的内部类
    class Node:
        def __init__(self, data, nextnode):
            self.val = data
            self.next = nextnode

    def __init__(self):
        """
        Initialize your data structure here.
        """
        ## 定义首结点
        self.head = None
        # 链表大小
        self.count = 0

    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
        """
        p = self.head
        # 如果当前链表里没有元素或者元素个数小于index,那么返回负一
        if self.count == 0 or index  > self.count - 1 :
            return -1
        else:
            i = 1
            while i <= index:
                p = p.next
                i += 1
            # print(self.count)
            return p.val
        
    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: void
        """
        # 创建一个新结点,使其指向原本的首结点
        node = self.Node(val, self.head)
       # 将node赋给新的首结点
        self.head = node
        # 增加一个首结点,链表大小加一
        self.count += 1
        

    def addAtTail(self, val):
        """
        Append a node of value val to the last element of the linked list.
        :type val: int
        :rtype: void
        """
        node = self.Node(val, None)
        if self.count == 0:
            self.head = node
            self.count += 1
        else:
            p = self.head
            while p.next != None:
                p = p.next
            p.next = node
            self.count += 1
                
        
            
        

    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: void
        """
        p = self.head
        post = self.head
        if index == self.count:
            # 如果插入点等于链表大小,那么相当于插入到最后
            self.addAtTail(val)
           # print(self.count)
        # 如果index大于链表大小那么不执行操作
        elif index > self.count:
            return 
        else:
            i = 1
            while i <= index:
                # 插入点位置的前面
                post = p
                # 插入点位置的后面
                p = p.next
                i += 1
            post.next = self.Node(val, p)
            self.count += 1
            
                
            
            

    def deleteAtIndex(self, index):
        """
        Delete the index-th node in the linked list, if the index is valid.
        :type index: int
        :rtype: void
        """
        p = self.head
        if self.count == 0 or index > (self.count - 1) or index < 0:
            return 
        else:
            i = 1
            while i <= index:
                post = p
                p = p.next
                i += 1
            post.next = p.next
            self.count -= 1
            
            
            


# 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)


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值