def push(self, new_data): new_node = Node(data = new_data) new_node.prev = None # at the beginning new_node.next = self.head if self.head is not None: self.head.prev = new_node self.head = new_node
def insert(self, index, data): """ Purpose: Insert new node at given index position :param index: node index to insert :param data: new node data :return: """ if index == 0 or self.head is None: self.push_front(data=data) return if index >= self.__len__(): self.push_back(data=data) return n = Node(data=data) i = 0 current = self.head prev = self.head while current: if index == i: prev.next = n n.next = current self.size += 1 return i += 1 prev = current current = current.next
def insert_before(self, next_node, new_data): if next_node is None: print("Error: given next_node is None") return new_node = Node(data = new_data) new_node.next = next_node new_node.prev = next_node.prev if new_node.prev is not None: # if new_node.prev (next_node.prev) is None it means we are at the beginning of the list new_node.prev.next = new_node next_node.prev = new_node
def insert_after(self, prev_node, new_data): if prev_node is None: print("Error: given prev_node is None") return new_node = Node(data = new_data) new_node.prev = prev_node new_node.next = prev_node.next if new_node.next is not None: # if new_node.next (prev_node.next) is None it means we are at the end of the list new_node.next.prev = new_node prev_node.next = new_node
def add_to_head(self, item): new_node = Node() new_node.item = item if not self.head: self.head = new_node else: old_head = self.head new_node.next = old_head self.head = new_node
def append(self, new_data): new_node = Node(data = new_data) new_node.next = None # at the last last = self.tail # if list is empty then this is the first node if last is None: new_node.prev = None self.head = new_node return last.next = new_node new_node.prev = last
def insert_at(self, index, value): ''' Inserts a value at the given position. index -- the position as an int. value -- an object. ''' if not isinstance(index, int): raise TypeError("index is not of type int") if index < 0 or index > self.size: raise LookupError("index out of bounds") new_node = Node() new_node.data = value if self.head is None: self.head = new_node self.tail = new_node self.size = 1 return if index == 0: new_node.next = self.head self.head.previous = new_node self.head = new_node self.size = 1 else: current_node = self.head for i in range(index-1): current_node = current_node.next new_node.previous = current_node new_node.next = current_node.next current_node.next = new_node if(new_node.next is None): self.tail = new_node self.size = 1
def push_front(self, data): """ Purpose: Push (add) node to front of SLL :param data: new node data :return: """ n = Node(data=data) if self.head is None: self.head = n self.size += 1 return old_head = self.head self.head = n n.next = old_head self.size += 1
# # return dummy # n1 = Node(1, None, None, None) n2 = Node(2, n1, None, None) n3 = Node(3, n2, None, None) n4 = Node(4, n3, None, None) n5 = Node(5, n4, None, None) n6 = Node(6, n5, None, None) n7 = Node(7, n6, None, None) n8 = Node(8, n7, None, None) n9 = Node(9, n8, None, None) n10 = Node(10, n9, None, None) n11 = Node(11, n10, None, None) n12 = Node(12, n11, None, None) n1.next = n2 n2.next = n3 n3.next = n4 n4.next = n5 n5.next = n6 n7.next = n8 n8.next = n9 n9.next = n10 n11.next = n12 n3.child = n7 n8.child = n11 head = Solution().flatten(n1)