707. 设计链表

单链表

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


class MyLinkedList(object):

    def __init__(self):
        self._head = Node(0)
        self._count = 0


    def get(self, index):
        """
        :type index: int
        :rtype: int
        """
        cur_node = self._head
        if 0 <= index < self._count:
            for _ in range(index+1):
                cur_node = cur_node.next
            return cur_node.val
        else:
            return -1


    def addAtHead(self, val):
        """
        :type val: int
        :rtype: None
        """
        return self.addAtIndex(0,val)


    def addAtTail(self, val):
        """
        :type val: int
        :rtype: None
        """
        return self.addAtIndex(self._count,val)

    def addAtIndex(self, index, val):
        """
        :type index: int
        :type val: int
        :rtype: None
        """
        if index < 0:
            index = 0
        if index > self._count:
            return
        
        add_node = Node(val)
        prev_node, cur_node = None, self._head
        for _ in range(index+1):
            prev_node, cur_node = cur_node, cur_node.next
        else:
            prev_node.next = add_node
            add_node.next = cur_node
            self._count += 1


    def deleteAtIndex(self, index):
        """
        :type index: int
        :rtype: None
        """
        if 0<= index <self._count:
            prev_node, cur_node = None, self._head
            for _ in range(index+1):
                prev_node, cur_node = cur_node, cur_node.next
            else:
                prev_node.next = cur_node.next
                cur_node.next = None
                self._count -= 1
        else:
            return


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

双链表
两个自己定义的功能函数:按 index 查找和在两个 Node 之间插入
功能函数插入的输入是两个 Node 而不使用 index,因为开头和结尾没必要查找

class Node(object):
    def __init__(self, val):
        self.val = val
        self.prev = None
        self.next = None

class MyLinkedList(object):

    def __init__(self):
        self._head = Node(0)
        self._tail = Node(0)
        self._head.next = self._tail
        self._tail.prev = self._head
        self._count = 0

    def _get_node(self, index):
        if index < self._count//2:
            node = self._head
            for _ in range(index+1):
                node = node.next
        else:
            node = self._tail
            for _ in range(self._count-index):
                node = node.prev
        return node


    def get(self, index):
        """
        :type index: int
        :rtype: int
        """
        if 0<= index < self._count:
            node = self._get_node(index)
            return node.val
        else:
            return -1

    def _insert(self, prev, next, val):
        add_node = Node(val)
        prev.next = add_node
        next.prev = add_node
        add_node.prev, add_node.next = prev, next
        self._count += 1


    def addAtHead(self, val):
        """
        :type val: int
        :rtype: None
        """
        return self._insert(self._head, self._head.next,val)


    def addAtTail(self, val):
        """
        :type val: int
        :rtype: None
        """
        return self._insert(self._tail.prev, self._tail, val)


    def addAtIndex(self, index, val):
        """
        :type index: int
        :type val: int
        :rtype: None
        """
        if index < 0:
            index = 0
        if index > self._count:
            return

        node = self._get_node(index)
        return self._insert(node.prev, node, val)


    def deleteAtIndex(self, index):
        """
        :type index: int
        :rtype: None
        """
        if 0<= index < self._count:
            node = self._get_node(index)
            node.prev.next = node.next
            node.next.prev = node.prev
            self._count -= 1
        else:
            return



# 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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值