Example #1
0
    return (head, tail)


def partition(root, x):
    head = head_tail = tail = tail_tail = None
    node = root
    while node:
        if node.value < x:
            head, head_tail = update_list(head, head_tail, node)
        else:
            tail, tail_tail = update_list(tail, tail_tail, node)
        node = node.next
    # Remove tail cycle
    if tail: tail_tail.next = None

    # Merge head and tail
    if head: head_tail.next = tail
    else: head = tail

    # Exist
    return head


print "PARTITION"
x = 50
root = create_list(N)
print print_list(root)
root = partition(root, x)
print print_list(root)
print_separator()
Example #2
0
#!/bin/python
from linked_list import create_list, print_list, print_separator, N, get_random_middle, get_last, visit_list

def get_instersection(root_1, root_2):
    visit_list(root_1)
    node = root_2
    while node and not node.visited:
        node.visited
        node = node.next
    return node

print "LOOP DETECTION"
root_1 = create_list(N)
root_2 = create_list(N)
random_node = get_random_middle(root_1, N)
last_node_2 = get_last(root_2)
last_node_2.next = random_node
print print_list(root_1)
print print_list(root_2)
intersect = get_instersection(root_1, root_2)
if intersect:
    print intersect.value
else:
    print "None"
print_separator()