def example(): ll = LinkedList.generate(100, 0, 9) print(ll) remove_dups(ll) print(ll) ll = LinkedList.generate(100, 0, 9) print(ll) remove_dups_followup(ll) print(ll)
# Pad the shorter list with zeros if len(ll_a) < len(ll_b): for i in range(len(ll_b) - len(ll_a)): ll_a.add_to_beginning(0) else: for i in range(len(ll_a) - len(ll_b)): ll_b.add_to_beginning(0) # Find sum n1, n2 = ll_a.head, ll_b.head result = 0 while n1 and n2: result = (result * 10) + n1.value + n2.value n1 = n1.next n2 = n2.next # Create new linked list ll = LinkedList() ll.add_multiple([int(i) for i in str(result)]) return ll if __name__ == "__main__": ll_a = LinkedList.generate(4, 0, 9) ll_b = LinkedList.generate(3, 0, 9) print(ll_a) print(ll_b) print(sum_lists(ll_a, ll_b)) print(sum_lists_followup(ll_a, ll_b))
def example(): ll_a = LinkedList.generate(4, 0, 9) ll_b = LinkedList.generate(3, 0, 9) print(ll_a) print(ll_b) print(sum_lists(ll_a, ll_b))
def example(): ll = LinkedList.generate(10, 0, 99) print(ll) partition(ll, ll.head.value) print(ll)
from chapter_02.linked_list import LinkedList def partition(ll, x): current = ll.tail = ll.head while current: nextNode = current.next current.next = None if current.value < x: current.next = ll.head ll.head = current else: ll.tail.next = current ll.tail = current current = nextNode # Error check in case all nodes are less than x if ll.tail.next is not None: ll.tail.next = None if __name__ == "__main__": ll = LinkedList.generate(10, 0, 99) print(ll) partition(ll, ll.head.value) print(ll)
) def test_remove_dupes(): for f in testable_functions: start = time.perf_counter() for _ in range(100): for values, expected in test_cases: expected = expected.copy() deduped = f(LinkedList(values)) assert deduped.values() == expected deduped.add(5) expected.append(5) assert deduped.values() == expected duration = time.perf_counter() - start print(f"{f.__name__} {duration * 1000:.1f}ms") if __name__ == "__main__": ll = LinkedList.generate(100, 0, 9) print(ll) remove_dups(ll) print(ll) ll = LinkedList.generate(100, 0, 9) print(ll) remove_dups_followup(ll) print(ll)