def add_lists(a: SinglyNode, b: SinglyNode, carry=0) -> SinglyNode: result = carry if a is None and b is None: if result != 0: return SinglyNode(carry) return None elif a is not None and b is not None: result += a.data + b.data elif a is not None: result += a.data else: result += b.data if result >= 10: carry = 1 result = result % 10 else: carry = 0 c = SinglyNode(result) c.next = add_lists(a.next, b.next, carry) return c
def delete_middle(node: SinglyNode): while node.next is not None: node.data = node.next.data if node.next.next is None: node.next = None break node = node.next
def push(self, data: Any): if self.top is None: self.top = SinglyNode(data) self.min_stack.append(data) else: if data <= self.min_stack[-1]: self.min_stack.append(data) new_node = SinglyNode(data) new_node.next = self.top self.top = new_node
def partition3(node: SinglyNode, x: Any) -> SinglyNode: head = node tail = node while node is not None: next_node = node.next if node.data < x: node.next = head head = node else: tail.next = node tail = node node = next_node tail.next = None return head_to_linked_list(head)
slow = head fast = head.next while True: if slow == fast: return True elif fast is None or fast.next is None: return False slow = slow.next fast = fast.next.next # Circular list x = SinglyNode(2) x.next = SinglyNode(1) x.next.next = SinglyNode(3) x.next.next.next = x Solution( is_circlular, [ Test( [x], True, None ), Test( [parse('1>2>3>1>2>4').head], False, None