Exemplo n.º 1
0
def floyd():
    linked_list = generate_linked_list(1000, 0.1)
    from floyd_algo_detection import detect_cycle

    return detect_cycle(linked_list)
Exemplo n.º 2
0
    _cur = dummy
    _start = dummy

    while _cur:
        _next = _cur.next
        if cnt == k:
            _end = _next
            _inner_cur = _start.next
            _inner_pre = _start
            while _inner_cur != _end:
                _inner_next = _inner_cur.next
                _inner_cur.next = _inner_pre
                _inner_pre = _inner_cur
                _inner_cur = _inner_next
            _last_node_of_cur_reversed_sub_linked_list = _start.next
            _last_node_of_cur_reversed_sub_linked_list.next = _end
            _start.next = _inner_pre
            _start = _last_node_of_cur_reversed_sub_linked_list

            cnt = 0

        cnt += 1
        _cur = _next

    return dummy.next


head = generate_linked_list(range(1, 10))
print(head)
print(reverse_k_nodes(head, 3))
from node import Node, generate_linked_list


def detect_cycle(head_node: Node) -> bool:
    pass


if __name__ == "__main__":
    head = generate_linked_list(100, 0.1)
    print(detect_cycle(head))
Exemplo n.º 4
0
                equal_tail.next = head
                equal_tail = equal_tail.next
        else:
            if large_head is None:
                large_head = larget_tail = head
            else:
                larget_tail.next = head
                larget_tail = larget_tail.next
        head = next

    if equal_tail:
        equal_tail.next = large_head
    else:
        equal_head = large_head

    if small_tail:
        small_tail.next = equal_head
    else:
        small_head = equal_head
    return small_head


head = generate_linked_list([7, 9, 1, 7, 5, 2, 5])
assert str(list_partition(head, 5)) == '1->2->5->5->7->9->7'
head = generate_linked_list([7, 9, 8, 7])
assert str(list_partition(head, 5)) == '7->9->8->7'
head = generate_linked_list([5, 5, 5, 5])
assert str(list_partition(head, 5)) == '5->5->5->5'
head = generate_linked_list([1, 2, 3, 1])
assert str(list_partition(head, 5)) == '1->2->3->1'
Exemplo n.º 5
0
    while _cur:
        #  x    pre   cur
        # [] <- []    [] -> [] -> []
        _next = _cur.next

        #  x    pre   cur   next
        # [] <- []    [] -> [] -> []

        _cur.next = _pre

        #  x    pre   cur   next
        # [] <- [] <- []    [] -> []

        _pre = _cur
        _cur = _next

        #  x     xx   pre   cur
        # [] <- [] <- []    [] -> []

    # when end of loop
        #  x     xx   xxx   xxxx  pre  cur
        # [] <- [] <- [] <- [] <- []   None
    return _pre


values = [1, 2, 3, 4, 5]
head = generate_linked_list(values)
print(head)
print(reverse_list(head))
        _cur = _cur.next

    if _from <= 0 or _from >= _to or _to > _len:
        return head

    _pre = None
    _cur = _start.next
    # the following part take ReverseList as reference
    while _cur != _end:
        _next = _cur.next
        _cur.next = _pre
        _pre = _cur
        _cur = _next
    # link the three parts: start + reversed_list + end
    # _end could be None, if _to is the last node of the original linked list
    _start.next.next = _end
    _start.next = _pre

    return dummy_head.next


head = generate_linked_list(range(1, 6))
assert str(revers_partial_list(head, 2, 3)) == '1->3->2->4->5'

head = generate_linked_list(range(1, 6))
assert str(revers_partial_list(head, 1, 5)) == '5->4->3->2->1'

head = generate_linked_list(range(1, 6))
assert str(revers_partial_list(head, 2, 4)) == '1->4->3->2->5'
Exemplo n.º 7
0
    _left_cur = head
    _right_cur = _right_start

    while _left_cur and _right_cur:
        if _left_cur.value != _right_cur.value:
            is_palindrome_flag = False
            break
        _left_cur = _left_cur.next
        _right_cur = _right_cur.next

    _cur = _right_start
    _pre = None
    while _cur:
        _next = _cur.next
        _cur.next = _pre
        _pre = _cur
        _cur = _next
    return is_palindrome_flag


head = generate_linked_list([1, 2, 3, 2, 1])
assert is_palindrome(head)
head = generate_linked_list([1, 2, 3, 1, 1])
assert not is_palindrome(head)

head = generate_linked_list([1, 2, 3, 2, 1])
assert is_palindrome3(head)

head = generate_linked_list([1, 2, 3, 3, 2, 1])
assert is_palindrome3(head)