leetcode题解记录-707设计链表(python3)

题目

设计链表
题目链接
官方题解

关键词

单链表,双链表

代码记录

方法一 单链表

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

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.godnode=ListNode(0)
    def get(self, index: int) -> int:
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        """
        if index<0:
            return -1
        cur_node=self.godnode.next
        cur_index=0
        while True:
            if not cur_node:
                return -1
            if index==cur_index:
                return cur_node.val
            else:
                cur_index += 1
                cur_node=cur_node.next

    def addAtHead(self, val: int) -> None:
        """
        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.
        """
        head = self.godnode.next
        cur = ListNode(val)
        cur.next=head
        self.godnode.next=cur

    def addAtTail(self, val: int) -> None:
        """
        Append a node of value val to the last element of the linked list.
        """
        tail=self.godnode
        cur = ListNode(val)
        while True:
            if tail and not tail.next:
                break
            else:
                tail = tail.next
        tail.next = cur

    def addAtIndex(self, index: int, val: int) -> None:
        """
        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.
        """
        cur_node = self.godnode
        cur_index = 0
        to_add = ListNode(val)
        add_flag=True
        index = max(0,index)
        while True:
            if cur_index==index:
                break
            else:
                cur_index += 1
                if not cur_node:
                    add_flag=False
                    break
                else:
                    cur_node = cur_node.next
        if add_flag:
            next_node = cur_node.next
            to_add.next = next_node
            cur_node.next = to_add

    def deleteAtIndex(self, index: int) -> None:
        """
        Delete the index-th node in the linked list, if the index is valid.
        """
        pre=self.godnode
        cur_index=0
        del_flag=True
        while True:
            if cur_index==index:
                break
            else:
                cur_index +=1
                if not pre or not pre.next or not pre.next.next:
                    del_flag=False
                    break
                else:
                    pre = pre.next
        if del_flag:
            if pre and pre.next:
                pre.next = pre.next.next

    def print_cur_list(self):
        root=self.godnode.next
        print('current list:')
        while root:
            print(str(root.val)+' ')
            root=root.next

方法二 双链表

class ListNode:
    def __init__(self,val):
        self.val=val
        self.next=None
        self.prev=None
class MyLinkedList:
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.godnode=ListNode(0)

    def get(self, index: int) -> int:
        """
        Get the value of the index-th node in the linked list. If the index is invalid, return -1.
        """
        if index<0:
            return -1
        cur_node=self.godnode.next
        while cur_node:
            if index==0:
                return cur_node.val
            else:
                cur_node=cur_node.next
                index -=1
        return -1

    def addAtHead(self, val: int) -> None:
        """
        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.
        """
        head=self.godnode.next
        cur_node=ListNode(val)
        cur_node.next=head
        if head:
            head.prev=cur_node
        self.godnode.next=cur_node
        cur_node.prev=self.godnode

    def addAtTail(self, val: int) -> None:
        """
        Append a node of value val to the last element of the linked list.
        """
        tail=self.godnode
        cur_node=ListNode(val)
        while tail and tail.next:
            tail=tail.next
        tail.next=cur_node
        cur_node.prev=tail

    def addAtIndex(self, index: int, val: int) -> None:
        """
        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.
        """
        if index<0:
            return
        cur_node=ListNode(val)
        cur=self.godnode
        while cur and cur.next:
            if index==0:
                break
            else:
                cur=cur.next
                index-=1
        if index==0:
            prev_node=cur
            next_node=cur.next
            prev_node.next=cur_node
            cur_node.next=next_node
            cur_node.prev=prev_node
            if next_node:
                next_node.prev=cur_node

    def deleteAtIndex(self, index: int) -> None:
        """
        Delete the index-th node in the linked list, if the index is valid.
        """
        if index<0:
            return
        cur=self.godnode.next
        while cur and cur.next:
            if index==0:
                break
            else:
                cur=cur.next
                index-=1
        if index==0:
            if cur:
                prev_node=cur.prev
                next_node=cur.next
                prev_node.next=next_node
                if next_node:
                    next_node.prev=prev_node

    def print_cur_list(self):
        root=self.godnode.next
        print('current list:')
        while root:
            print(str(root.val)+' ')
            root=root.next
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值