classNode: def__init__(self,val): self.next = None self.val = val classMyLinkedList(object):
def__init__(self): """ Initialize your data structure here. """ self.head = Node(-1) self.count = 0 defget(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 """ if index>=0and index<self.count: cur = self.head.next for i inrange(index): cur = cur.next return cur.val else: return -1 defaddAtHead(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 """ self.addAtIndex(0,val) defaddAtTail(self, val): """ Append a node of value val to the last element of the linked list. :type val: int :rtype: None """ self.addAtIndex(self.count,val)
defaddAtIndex(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 """ if index >= 0and index <= self.count: cur = self.head newNode = Node(val) for i inrange(index): cur = cur.next if cur.next == None: cur.next = newNode else: newNode.next = cur.next cur.next = newNode self.count += 1 returnNone
defdeleteAtIndex(self, index): """ Delete the index-th node in the linked list, if the index is valid. :type index: int :rtype: None """ if index>=0and index<self.count: cur = self.head for i inrange(index): cur = cur.next cur.next = cur.next.next self.count -= 1
defget(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 """ if index<0or index>=self.count: return -1 if index<self.count/2: cur = self.head for _ inrange(index+1): cur = cur.next else: cur = self.tail for _ inrange(self.count-index): cur = cur.pre return cur.val
defaddAtHead(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 """ self.addAtIndex(0,val) defaddAtTail(self, val): """ Append a node of value val to the last element of the linked list. :type val: int :rtype: None """ self.addAtIndex(self.count,val)
defaddAtIndex(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 """ if index>self.count: returnNone if index<0: index = 0 if index<self.count/2: cur=self.head for _ inrange(index): cur = cur.next else: cur = self.tail for _ inrange(self.count-index+1): cur = cur.pre newNode = Node(val) newNode.next = cur.next cur.next.pre = newNode cur.next = newNode newNode.pre = cur self.count += 1
defdeleteAtIndex(self, index): """ Delete the index-th node in the linked list, if the index is valid. :type index: int :rtype: None """ if index>=self.count or index<0: returnNone if index<self.count/2: cur=self.head for _ inrange(index): cur = cur.next else: cur = self.tail for _ inrange(self.count-index+1): cur = cur.pre cur.next = cur.next.next cur.next.pre = cur self.count -= 1