classNode(object): def__init__(self, x): self.val = x self.next = None
classMyLinkedList:
def__init__(self): """ Initialize your data structure here. """ self.head = Node(0)
defget(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 node = self.head for _ in range(index + 1): if node.next isnotNone:node = node.next else: return-1 return node.val defaddAtHead(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. """ new = Node(val) new.next = self.head.next self.head.next = new
defaddAtTail(self, val: int) -> None: """ Append a node of value val to the last element of the linked list. """ node = self.head while node.next isnotNone: node = node.next node.next = Node(val)
defaddAtIndex(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. """ node = self.head for i in range(index): if node isNone: return node = node.next if node isNone: node = Node(val) else: new = Node(val) new.next = node.next node.next = new
defdeleteAtIndex(self, index: int) -> None: """ Delete the index-th node in the linked list, if the index is valid. """ if index < 0: return node = self.head for _ in range(index): node = node.next if node.next isnotNone: node.next = node.next.nex