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
示例#2
0
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():
    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)
示例#4
0
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 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
示例#6
0
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
示例#7
0
def simple_test():
    L = None
    assert not remove_duplicates(L)
    L = ListNode(123)
    result = remove_duplicates(L)
    assert result is L
    L.next = ListNode(123)
    result = remove_duplicates(L)
    assert not result.next

    # Creating an invalid input, 123 -> 124 -> 123, algo will not detect dups!
    L.next = ListNode(124, ListNode(123))
    result = remove_duplicates(L)
    assert L.data == 123 and L.next.data == 124 and L.next.next.data == 123
示例#8
0
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 create_list(n):
    head = None
    for i in reversed(range(n)):
        curr = ListNode(i, None)
        curr.next = head
        head = curr
    return head
示例#10
0
def create_list(n):
    head = None
    for i in reversed(range(n)):
        curr = ListNode(i, None)
        curr.next = head
        head = curr
    return head
示例#11
0
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 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 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
示例#14
0
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
示例#15
0
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')