def main(): for _ in range(1000): n = int(sys.argv[1]) if len(sys.argv) == 2 else random.randint(1, 1000) head = None for i in reversed(range(n + 1)): curr = ListNode(i, head) head = curr curr = head if curr: while curr.next: curr = curr.next curr.next = head # make the list as a circular list. res = find_median_sorted_circular_linked_list(head.next) print(res) assert res == 0.5 * n # Test identical list. head = None for i in range(10): curr = ListNode(5, head) head = curr curr = head if curr: while curr.next: curr = curr.next curr.next = head # make the list as a circular list. assert 5 == find_median_sorted_circular_linked_list(head)
def main(): simple_test() for _ in range(10000): F = None L = None if len(sys.argv) == 3: n = int(sys.argv[1]) m = int(sys.argv[2]) elif len(sys.argv) == 2: n = int(sys.argv[1]) m = int(sys.argv[1]) else: n = random.randint(0, 99) m = random.randint(0, 99) for i in reversed(range(1, n + 1)): temp = ListNode(i, None) temp.next = F F = temp for j in reversed(range(1, m + 1)): temp = ListNode(j, None) temp.next = L L = temp sorted_head = merge_two_sorted_lists(F, L) count = 0 pre = float('-inf') while sorted_head: assert pre <= sorted_head.data pre = sorted_head.data sorted_head = sorted_head.next count += 1 # Make sure the merged list have the same number of nodes as F and L. assert count == n + m
def create_list(n): head = None for i in reversed(range(n)): curr = ListNode(i, None) curr.next = head head = curr return head
def create_list(n): head = None for i in reversed(range(n)): curr = ListNode(i, None) curr.next = head head = curr return head
def main(): # L1: 1->2->3->None L1 = ListNode(1, ListNode(2, ListNode(3, None))) L2 = L1.next.next assert overlapping_no_cycle_lists(L1, L2).data == 3 # L2: 4->5->None L2 = ListNode(4, ListNode(5, None)) assert not overlapping_no_cycle_lists(L1, L2)
def simple_test(): L = ListNode(1) assert cyclically_right_shift_list(L, 2) is L L.next = ListNode(2) result = cyclically_right_shift_list(L, 2) assert result is L result = cyclically_right_shift_list(L, 3) assert result.next is L
def main(): L = ListNode(1, ListNode(2, ListNode(3, None))) L = remove_kth_last(L, 2) assert L.data == 1 and L.next.data == 3 L = remove_kth_last(L, 2) assert L.data == 3 and L.next is None L = remove_kth_last(L, 1) assert not L
def simple_test(): L = ListNode(1) assert cyclically_right_shift_list(L, 2) is L L.next = ListNode(2) result = cyclically_right_shift_list(L, 2) assert result is L result = cyclically_right_shift_list(L, 3) assert result.next is L
def main(): simple_test() L = ListNode(1, ListNode(2, ListNode(3, None))) result = cyclically_right_shift_list(L, 2) assert result.data == 2 and result.next.data == 3 and result.next.next.data == 1 and not result.next.next.next while result: print(result.data) result = result.next
def add_two_numbers(L1, L2): place_iter = dummy_head = ListNode() carry = 0 while L1 or L2 or carry: val = carry + (L1.data if L1 else 0) + (L2.data if L2 else 0) L1 = L1.next if L1 else None L2 = L2.next if L2 else None place_iter.next = ListNode(val % 10) carry, place_iter = val // 10, place_iter.next return dummy_head.next
def main(): L1 = ListNode(1, ListNode(2, ListNode(3, None))) print('before reverse') print_list(L1) newhead = reverse_linked_list(L1) print('\nafter reverse') assert newhead.data == 3 and newhead.next.data == 2 and newhead.next.next.data == 1 print_list(newhead) newhead = reverse_linked_list(newhead) assert newhead.data == 1 and newhead.next.data == 2 and newhead.next.next.data == 3 print('\nafter another reverse') print_list(newhead)
def simple_test(): L0 = ListNode(42) L0.next = L0 assert has_cycle(L0) L1, L2 = ListNode(42), ListNode(42) L1.next, L2.next = L2, L1 assert has_cycle(L1) is L1 assert has_cycle(L2) is L2 L2.next = None assert has_cycle(L1) is None assert has_cycle(L2) is None
def main(): simple_test() L3 = ListNode(3, None) L2 = ListNode(2, L3) L1 = ListNode(1, L2) # Should output 'L1 does not have cycle.' assert has_cycle(L1) is None print('L1', 'has' if has_cycle(L1) else 'does not have', 'cycle.') # Make it a cycle L3.next = L2 # Should output 'L1 has cycle, at node has value 2' assert has_cycle(L1) is not None assert has_cycle(L1).data == 2 temp = has_cycle(L1) if temp: print('L1 has cycle, at node has value', temp.data) else: print('L1 does not have cycle') P3 = ListNode(44, None) P2 = ListNode(33, P3) P1 = ListNode(22, P2) print_linked_list(P1)
def even_odd_merge(L): if not L: return L even_dummy_head, odd_dummy_head = ListNode(0), ListNode(0) tails, turn = [even_dummy_head, odd_dummy_head], 0 while L: tails[turn].next = L L = L.next tails[turn] = tails[turn].next turn ^= 1 # Alternate between even and odd. tails[1].next = None tails[0].next = odd_dummy_head.next return even_dummy_head.next
def main(): simple_test() L = ListNode(2, ListNode(2, ListNode(2, ListNode(2, ListNode(2, None))))) pre = None result = remove_duplicates(L) count = 0 while result: count += 1 if pre: assert pre.data != result.data print(result.data) pre = result result = result.next assert count == 1
def main(): small_test() # L1: 1->2->3->null L1 = ListNode(1, ListNode(2, ListNode(3, None))) L2 = L1.next.next assert overlapping_lists(L1, L2).data == 3 # L2: 4->5->null L2 = ListNode(4, ListNode(5, None)) assert not overlapping_lists(L1, L2) L1.next.next.next = L1 assert not overlapping_lists(L1, L2) L2.next.next = L2 assert not overlapping_lists(L1, L2) L2.next.next = L1 assert overlapping_lists(L1, L2)
def main(): L = ListNode(1, ListNode(2, ListNode(3, ListNode(4, ListNode(5, None))))) if len(sys.argv) == 2: k = int(sys.argv[1]) else: k = 2 result = reverse_k(L, k) assert (result.data == 2 and result.next.data == 1 and result.next.next.data == 4 and result.next.next.next.data == 3 and result.next.next.next.next.data == 5 and result.next.next.next.next.next is None) while result: print(result.data) result = result.next
def reverse_k(L, k): dummy_head = ListNode(0, L) sublist_predecessor = dummy_head sublist_head = dummy_head.next sublist_successor = dummy_head sublist_tail = dummy_head.next while sublist_head: # Identify the tail of sublist of k nodes to be reversed. num_remaining = k while num_remaining: sublist_successor = sublist_tail sublist_tail = sublist_tail.next num_remaining -= 1 if not sublist_tail: break if num_remaining > 0: # Specification says not to reverse. return dummy_head.next sublist_successor.next = None reverse_linked_list(sublist_head) # Splice the reversed sublist. sublist_predecessor.next = sublist_successor # Go on to the head of next sublist. sublist_predecessor = sublist_head sublist_head.next = sublist_tail sublist_head = sublist_tail sublist_successor = None return dummy_head.next
def main(): simple_test() L = ListNode(1, ListNode(4, ListNode(3, ListNode(2, ListNode(5, None))))) x = 4 result = list_pivoting(L, x) print() before_x = True while result: if result.data >= x: before_x = False if before_x: assert result.data < x else: assert result.data >= x print(result.data) result = result.next
def simple_test(): L1, L2 = None, None assert merge_two_sorted_lists(L1, L2) is None L1 = ListNode(123) result = merge_two_sorted_lists(L1, L2) assert result.data == 123 and result.next is None L2 = ListNode(123) L1 = None result = merge_two_sorted_lists(L1, L2) assert result.data == 123 and result.next is None L1 = ListNode(-123) L2 = ListNode(123) result = merge_two_sorted_lists(L1, L2) assert result.data == -123 and result.next.data == 123 and result.next.next is None
def simple_test(): L = ListNode(0) result = even_odd_merge(L) assert result.data == 0 assert not result.next L.next = ListNode(1) result = even_odd_merge(L) assert result.data == 0 assert result.next.data == 1 assert not result.next.next L.next.next = ListNode(2) result = even_odd_merge(L) assert result.data == 0 assert result.next.data == 2 assert result.next.next.data == 1 assert not result.next.next.next
def simple_test(): L = ListNode(0) result = even_odd_merge(L) assert result.data == 0 assert not result.next L.next = ListNode(1) result = even_odd_merge(L) assert result.data == 0 assert result.next.data == 1 assert not result.next.next L.next.next = ListNode(2) result = even_odd_merge(L) assert result.data == 0 assert result.next.data == 2 assert result.next.next.data == 1 assert not result.next.next.next
def main(): L3 = ListNode(3, None) L2 = ListNode(2, L3) L1 = ListNode(1, L2) # Should output 'L1 does not have cycle.' assert has_cycle(L1) is None print('L1', 'has' if has_cycle(L1) else 'does not have', 'cycle.') # Make it a cycle L3.next = L2 # Should output 'L1 has cycle, at node has value 2' assert has_cycle(L1) is not None assert has_cycle(L1).data == 2 temp = has_cycle(L1) if temp: print('L1 has cycle, at node has value', temp.data) else: print('L1 does not have cycle')
def list_pivoting(L, x): less_head = less_iter = ListNode() equal_head = equal_iter = ListNode() greater_head = greater_iter = ListNode() # Populates the three lists. while L: if L.data < x: less_iter.next = L less_iter = less_iter.next elif L.data == x: equal_iter.next = L equal_iter = equal_iter.next else: # L.data > x. greater_iter.next = L greater_iter = greater_iter.next L = L.next # Combines the three lists. greater_iter.next = None equal_iter.next = greater_head.next less_iter.next = equal_head.next return less_head.next
def merge_two_sorted_lists(L1, L2): # Creates a placeholder for the result. dummy_head = tail = ListNode() while L1 and L2: if L1.data < L2.data: tail.next, L1 = L1, L1.next else: tail.next, L2 = L2, L2.next tail = tail.next tail.next = L2 or L1 return dummy_head.next
def remove_kth_last(L, k): dummy_head = ListNode(0, L) first = dummy_head.next for _ in range(k): first = first.next second = dummy_head while first: first, second = first.next, second.next # second points to the (k + 1)-th last node, deletes its successor. second.next = second.next.next return dummy_head.next
def main(): tail = None length = 0 for i in reversed(range(4)): node = ListNode(i, tail) if tail: tail.prev = node tail = node length += 1 tree = build_bst_from_sorted_doubly_list(tail, length) inorder_traversal(tree, -1, 0)
def main(): head = None if len(sys.argv) > 2: for i in sys.argv[1:]: curr = ListNode(int(i), head) head = curr else: n = int(sys.argv[1]) if len(sys.argv) == 2 else random.randint(1, 1000) for i in reversed(range(n + 1)): curr = ListNode(i, head) head = curr curr = zipping_linked_list(head) idx = 0 while curr: if len(sys.argv) <= 2: if idx & 1: assert pre + curr.data == n idx += 1 print(curr.data) pre = curr.data curr = curr.next
def reverse_sublist(L, start, finish): dummy_head = sublist_head = ListNode(0, L) for _ in range(1, start): sublist_head = sublist_head.next # Reverses sublist. sublist_iter = sublist_head.next for _ in range(finish - start): temp = sublist_iter.next sublist_iter.next, temp.next, sublist_head.next = ( temp.next, sublist_head.next, temp) return dummy_head.next
def main(): for _ in range(1000): n = int(sys.argv[1]) if len(sys.argv) == 2 else random.randint(1, 1000) head = None for i in reversed(range(n + 1)): curr = ListNode(i, head) head = curr curr = head if curr: while curr.next: curr = curr.next curr.next = head # make the list as a circular list. res = find_median_sorted_circular_linked_list(head.next) print(res) assert res == 0.5 * n # Test identical list. head = None for i in range(10): curr = ListNode(5, head) head = curr curr = head if curr: while curr.next: curr = curr.next curr.next = head # make the list as a circular list. assert 5 == find_median_sorted_circular_linked_list(head)
def main(): simple_test() for _ in range(10000): F = None L = None if len(sys.argv) == 3: n = int(sys.argv[1]) m = int(sys.argv[2]) elif len(sys.argv) == 2: n = int(sys.argv[1]) m = int(sys.argv[1]) else: n = random.randint(0, 99) m = random.randint(0, 99) for i in reversed(range(1, n + 1)): temp = ListNode(i, None) temp.next = F F = temp for j in reversed(range(1, m + 1)): temp = ListNode(j, None) temp.next = L L = temp sorted_head = merge_two_sorted_lists(F, L) count = 0 pre = float('-inf') while sorted_head: assert pre <= sorted_head.data pre = sorted_head.data sorted_head = sorted_head.next count += 1 # Make sure the merged list have the same number of nodes as F and L. assert count == n + m
def insertion_sort(L): dummy_head = ListNode(0, L) while L and L.next: if L.data > L.next.data: target, pre = L.next, dummy_head while pre.next.data < target.data: pre = pre.next temp, pre.next, L.next = pre.next, target, target.next target.next = temp else: L = L.next return dummy_head.next
def main(): L = ListNode(2, ListNode(4, ListNode(3, None))) insert_after(L, ListNode(1, None)) check_answer(L, [2, 1, 4, 3]) insert_after(L.next.next, ListNode(10, None)) check_answer(L, [2, 1, 4, 10, 3]) insert_after(L.next.next.next.next, ListNode(-1, None)) check_answer(L, [2, 1, 4, 10, 3, -1])
def main(): simple_test() L = ListNode(1, ListNode(2, ListNode(3, None))) result = reverse_sublist(L, 3, 3) assert (result.data == 1 and result.next.data == 2 and result.next.next.data == 3 and not result.next.next.next) while result: print(result.data) result = result.next result = reverse_sublist(L, 2, 3) assert (result.data == 1 and result.next.data == 3 and result.next.next.data == 2 and not result.next.next.next) while result: print(result.data) result = result.next L = ListNode(3, ListNode(5, None)) result = reverse_sublist(L, 1, 2) assert result.data == 5 and result.next.data == 3 and not result.next.next while result: print(result.data) result = result.next
def simple_test(): L0 = ListNode(42) L0.next = L0 assert has_cycle(L0) L1, L2 = ListNode(42), ListNode(42) L1.next, L2.next = L2, L1 assert has_cycle(L1) is L1 assert has_cycle(L2) is L2 L2.next = None assert has_cycle(L1) is None assert has_cycle(L2) is None
def main(): for _ in range(10000): n = int(sys.argv[1]) if len(sys.argv) == 2 else random.randrange(100) L = None for _ in range(n): L = ListNode(random.randrange(100), L) sorted_head = stable_sort_list(L) count = 0 pre = float('-inf') while sorted_head: assert pre <= sorted_head.data pre = sorted_head.data sorted_head = sorted_head.next count += 1 assert count == n