def append_node(self, data): """ Add a Node to the Linked List :param data: :return: """ if not self.head: self.head = Node(data) return else: current = self.head while current.next: current = current.next current.next = Node(data)
def enqueue(self, value): node_ = Node(None, value) if self.empty(): self.head = self.tail = node_ else: self.tail.set_next(node_) self.tail = node_
def append(self, item): current = self.head temp = Node(item) while current.get_next() is not None: current = current.get_next() current.set_next(temp)
def insert(self, index, value): if index < 0 or index >= self.size: return new_node = Node(None, value) if index == 0: new_node.set_next(self.head) self.head = new_node self.size += 1 return current = self.head while index - 1 is not 0: current = current.get_next() index -= 1 current_next = current.get_next() new_node.set_next(current_next) current.set_next(new_node) if new_node.get_next() is None: self.tail = new_node self.size += 1
def push_back(self, value): node_ = Node(None, value) if self.empty(): self.head = node_ self.tail = node_ else: self.tail.set_next(node_) self.tail = node_ self.size += 1
def add_node(self, data: Any) -> Node: """ adds a node with the given data at the end of the linked list and returns the update tail node :param data: holds the value for the node :return tail node: pointer to last node """ node = self.__tail node.next_node = Node(data=data) self.__tail = node.next_node return self.__tail
def add_all_nodes(self, data_list: List): """ adds the elements for the given `data_list` in a singly linked list :param data_list: list of values that needs to be added to the list :return: status_flag and message """ node = self.__tail success: bool = False message: str = "" try: for data in data_list: node.next_node = Node(data=data) node = node.next_node success = True message = "added successfully" except RuntimeError as e: success = False message = f"error in adding node:{e}" finally: return success, message
def insert(self, item, pos): node = Node(item) current = self.head previous = None index = 0 while current.get_next() is not None: if index == pos: if previous is not None: previous.set_next(node) node.set_next(current) else: node.set_next(current) self.head = node return else: previous = current current = current.get_next() index += 1 # If item is to be inserted at the last index previous.set_next(node) node.set_next(None)
def add(self, item): current = self.head previous = None stop = False while current is not None and not stop: # Make use of sorted property to stop traversal early if current.get_data() > item: stop = True else: previous = current current = current.get_next() node = Node(item) # if item has to be inserted in first position # i.e. previous is None, reset head to new item if previous is None: node.set_next(self.head) self.head = node # Normal case: prev to item, item to current else: previous.set_next(node) node.set_next(current)
class LinkedList: def __init__(self): self.head = None self.tail = None self.size = 0 def size(self): return self.size def empty(self): return self.size == 0 def value_at(self, index): if index < 0 or index >= self.size: return None current = self.head while index is not 0: current = current.get_next() index -= 1 return current.get_value() def push_front(self, value): self.head = Node(self.head, value) self.size += 1 def pop_front(self): if self.empty(): return None value = self.head.get_value() self.head = self.head.get_next() if self.head is None: self.tail = None self.size -= 1 return value def push_back(self, value): node_ = Node(None, value) if self.empty(): self.head = node_ self.tail = node_ else: self.tail.set_next(node_) self.tail = node_ self.size += 1 def pop_back(self): if self.empty(): return None current = self.head while current.get_next() is not None: next_ = current.get_next() if next_ == self.tail: value = next_.get_value() current.set_next(None) self.tail = current self.size -= 1 return value current = current.get_next() return self.pop_front() def front(self): return self.head.get_value() def back(self): return self.tail.get_value() def insert(self, index, value): if index < 0 or index >= self.size: return new_node = Node(None, value) if index == 0: new_node.set_next(self.head) self.head = new_node self.size += 1 return current = self.head while index - 1 is not 0: current = current.get_next() index -= 1 current_next = current.get_next() new_node.set_next(current_next) current.set_next(new_node) if new_node.get_next() is None: self.tail = new_node self.size += 1 def erase(self, index): if index < 0 or index >= self.size: return if index == 0: self.pop_front() return if index == self.size - 1: self.pop_back() return current = self.head while index - 1 is not 0: index -= 1 current = current.get_next() current.set_next(current.get_next().get_next()) self.size -= 1 if current.get_next() is None: self.tail = current def value_n_from_end(self, n): if n < 0 or n >= self.size: return None if n == 0: return self.tail.get_value() elif n == self.size - 1: return self.head.get_value() current = self.head while n is not 0: current = current.get_next() n -= 1 return current.get_value() def remove_value(self, value): if self.head.get_value() == value: self.pop_front() elif self.tail.get_value() == value: self.pop_back() else: current = self.head while current.get_next() is not None: if current.get_value() == value: current.set_next(current.get_next().get_next()) if current.get_next() is None: self.tail = current def reverse(self): if self.empty(): return prev = None current = self.head self.tail = current while current is not None: next_ = current.get_next() current.set_next(prev) prev = current current = next_ self.head = prev
def push_front(self, value): self.head = Node(self.head, value) self.size += 1
def __init__(self, data: Any = None): self.__head: Node = Node(data=data) self.__tail: Node = self.__head self.__length: int = 1
def add(self, item): # add item to first item (i.e. head) # reset head to point to the new first item in the list temp = Node(item) temp.set_next(self.head) self.head = temp