[Leetcode with python] 707. Design Linked List (# Linked list)

本文详细解析了LeetCode上设计链表的问题,通过定义Node和MyLinkedList类,实现链表的基本操作如获取、添加和删除节点。文章强调了在编程中如何设计类和方法的重要性。

题目

https://leetcode.com/problems/design-linked-list/
在这里插入图片描述

解题

  • 分情况讨论的思维(if-else),要养成习惯
  • 如何去设计/定义一个class,以及class里面的变量和方法
class MyLinkedList(object):

    class Node(object):
        def __init__(self, x, nextNode):
            self.val = x
            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
        if self.count == 0 or index > self.count - 1:
            return -1
        i = 0
        while i < index:
            p = p.next
            i += 1
        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: None
        """
        node = self.Node(val, self.head)
        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: None
        """
        node = self.Node(val, None)
        if self.count == 0:
            self.head = node
        else:
            p = self.head
            i = 0
            while i < self.count -1:
                p = p.next
                i += 1
            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: None
        """
        p = self.head
        if index == self.count:
            self.addAtTail(val)
        elif index > self.count:
            return
        else:
            i = 0
            while i < index - 1:
                p = p.next
                i += 1
            node = self.Node(val, p.next)
            p.next = node
            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: None
        """
        p = self.head
        if index >= self.count:
            return
        else:
            i = 0
            while i < index - 1:
                p = p.next
                i += 1
            p.next = p.next.next
            self.count -= 1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值