def sum_lists(list_one_head, list_two_head): pointer_one = list_one_head pointer_two = list_two_head sum_list_start = 0 carry = 0 sum_head = None pointer_sum = None while pointer_one is not None or pointer_two is not None: if pointer_one is not None and pointer_two is not None: digit_sum = pointer_one.value + pointer_two.value + carry elif pointer_one is not None: digit_sum = pointer_one.value + carry else: digit_sum = pointer_two.value + carry print("Digit Sum: ", digit_sum) carry, new_digit = divmod(digit_sum, 10) print("Carry: ", carry, "New digit: ", new_digit) # Add resulting digit to 'sum' linked list and move pointer to that digit if sum_list_start == 0: sum_head = LinkedListNode(new_digit) pointer_sum = sum_head sum_list_start = 1 else: pointer_sum.next = LinkedListNode(new_digit) pointer_sum = pointer_sum.next # Move to the next digit in the lists if pointer_one is not None: pointer_one = pointer_one.next if pointer_two is not None: pointer_two = pointer_two.next return sum_head
def test_partition_short(self): a = LinkedListNode(9) b = LinkedListNode(7) c = LinkedListNode(2) d = LinkedListNode(1) a.next = b b.next = c c.next = d h = partition_linked_list(a, 5) print(h, h.next.value, h.next.next.value, h.next.next.next.value)
def reverse_and_clone(node): prev = None while node is not None: current = LinkedListNode(node.value) current.next = prev prev = current node = node.next return prev
def test_sll_palindrome(self): a = LinkedListNode(9) b = LinkedListNode(7) c = LinkedListNode(6) d = LinkedListNode(5) e = LinkedListNode(6) f = LinkedListNode(7) g = LinkedListNode(9) # List 1: 679 a.next = b b.next = c c.next = d d.next = e e.next = f f.next = g self.assertEqual(is_sll_palindrome_reverse(a), True)
def test_partition(self): a = LinkedListNode(9) b = LinkedListNode(7) c = LinkedListNode(6) d = LinkedListNode(5) e = LinkedListNode(4) f = LinkedListNode(3) g = LinkedListNode(2) # List 1: 679 a.next = b b.next = c # List 2: 2345 d.next = e e.next = f f.next = g # Sum: h = sum_lists(a, d) print(h.value, h.next.value, h.next.next.value, h.next.next.next.value)
def test_partition(self): a = LinkedListNode(9) b = LinkedListNode(7) c = LinkedListNode(6) d = LinkedListNode(5) e = LinkedListNode(4) f = LinkedListNode(3) g = LinkedListNode(2) a.next = b b.next = c c.next = d d.next = e e.next = f f.next = g h = partition_linked_list(a, 5) fourh = h.next.next.next print(h.value, h.next.value, h.next.next.value, fourh.value, fourh.next.value, fourh.next.next.value, fourh.next.next.next.value)
def test_solution(self): a = LinkedListNode(1) b = LinkedListNode(2) c = LinkedListNode(3) d = LinkedListNode(4) e = LinkedListNode(5) f = LinkedListNode(6) g = LinkedListNode(7) a.next = b b.next = c c.next = d d.next = e e.next = f f.next = g solution(e) self.assertEqual(d.next.value, 6) self.assertEqual(d.next.next, g) self.assertEqual(f.next, g)
def push(self, item): """Adds an item to the stack.""" second = self.top self.top = LinkedListNode(item) self.top.next = second