Участник:Ковальков Антон М05-903б/Design Linked List

Материал из DISCOPAL
Перейти к: навигация, поиск

https://leetcode.com/problems/design-linked-list/

Python3

class Node:
        def __init__(self, val=None, nnext=None):
            self.val = val
            self.next = nnext
 
 
class MyLinkedList:
 
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.head = Node()
 
 
    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
        current_index = 0
        current_node = self.head.next
        while current_node is not None:
            if current_index == index:
                return current_node.val
            current_index += 1
            current_node = current_node.next
        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.
        """
        self.head.next = Node(val, self.head.next)
 
 
    def addAtTail(self, val: int) -> None:
        """
        Append a node of value val to the last element of the linked list.
        """
        current_node = self.head
        while current_node.next is not None:
            current_node = current_node.next
        current_node.next = Node(val)
 
 
    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
        current_index = 0
        current_node = self.head
        while current_node is not None:
            if current_index == index:
                current_node.next = Node(val, current_node.next)
                return
            current_index += 1
            current_node = current_node.next
 
 
    def deleteAtHead(self) -> None:
        if self.head.next:
            self.head.next = self.head.next.next
 
    def deleteAtIndex(self, index: int) -> None:
        """
        Delete the index-th node in the linked list, if the index is valid.
        """
        if index < 0:
            return
        if index == 0:
            return self.deleteAtHead()
        if not self.head.next:
            return
        current_index = 0
        prev_node = self.head
        current_node = self.head.next
        while current_node is not None:
            if current_index == index:
                prev_node.next = current_node.next  # todo
                return
            current_index += 1
            prev_node, current_node = current_node, current_node.next
 
 
 
# 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)