def insert_at_begining(self, data): new_node = Node(data) if self.get_head() == None and self.get_tail() == None: self.set_head(new_node) self.set_tail(new_node) else: new_node.set_next(self.get_head()) self.set_head(new_node)
def insert_at_middle(self, pos, data): if pos < 1: print("Position does not exist") return None else: if pos == 1: self.insert_at_begining(data) elif pos == self.length_linkedlist() + 1: self.insert_at_end(data) elif pos > self.length_linkedlist() + 1: print("Position does not exist") else: new_node = Node(data) temp = self.get_head() i = 1 while i < pos - 1: temp = temp.get_next() i += 1 new_node.set_next(temp.get_next()) temp.set_next(new_node)