def insertStart(self, data): newNode = Node(data) if not self.head: self.head = newNode else: newNode.nextNode = self.head self.head = newNode
def insertStart(self,data): newNode = Node(data); if not self.head : self.head = newNode; else: newNode.nextNode = self.head; self.head = newNode;
def insertStart(self, data): newNode = Node(data); self.counter += 1; if not self.head: self.head = newNode; else: newNode.nextNode = self.head; self.head = newNode;
def insert_start(self, data): self.counter += 1 newNode = Node(data) if not self.head: self.head = newNode else: newNode.next = self.head self.head = newNode # True表示插入成功 return True
def insertStart(self, data): #O(1) self.counter += 1 newNode = Node(data) if not self.head: # If it is a first element self.head = newNode else: newNode.nextNode = self.head self.head = newNode
def insertEnd(self, data): newNode = Node(data) actualNode = self.head while actualNode.nextNode is not None: actualNode = actualNode.nextNode actualNode.nextNode = newNode
def insertEnd(self, data): if self.head is None: self.insertStart(data) return self.counter += 1 newNode = Node(data) actualNode = self.head while actualNode.nextNode is not None: actualNode = actualNode.nextNode actualNode.nextNode = newNode
def insert_end(self, data): if self.head is None: self.insert_start(data) return True self.counter += 1 newNode = Node(data) actualNode = self.head while actualNode.next is not None: actualNode = actualNode.next actualNode.next = newNode # True表示插入成功 return True