Exemple #1
0
def _print():
    a1 = [1, 2, 1, 3, 1, 2, 4, 5]
    l1 = create_list(a1)
    delete_duplicates2(l1)
    a2 = [1, 1, 1]
    l2 = create_list(a2)
    delete_duplicates2(l2)
    print(restore_list(l1))
    print(restore_list(l2))
Exemple #2
0
def _print():
    a1 = [1, 2, 3, 4, 5]
    l1 = create_list(a1)
    p = l1
    while p.next is not None:
        p = p.next
    p.next = l1.next.next
    res = find_beginning(l1)
    print(res.data)
    a2 = [1, 2]
    l2 = create_list(a2)
    l2.next.next = l2.next
    print(find_beginning(l2).data)
Exemple #3
0
def add_lists(l1, l2):
    """
    Two lists contain digits stored in reverse order
    """
    p = res = None
    carry = 0
    while l1 is not None or l2 is not None:
        data1 = 0
        data2 = 0
        if l1 is not None:
            data1 = l1.data
            l1 = l1.next
        if l2 is not None:
            data2 = l2.data
            l2 = l2.next
        data = (data1 + data2 + carry) % 10
        carry = 1 if data1 + data2 + carry >= 10 else 0
        if res is None:
            res = create_list([data])
            p = res
        else:
            p.next = ListNode(data)
            p = p.next
    if carry > 0:
        p.next = ListNode(1)
    return res
Exemple #4
0
def _print():
    a1 = [2]
    a2 = [1, 2, 1]
    a3 = [1, 2, 3, 4, 1]
    a4 = [2, 0, 2]
    l1 = create_list(a1)
    l2 = create_list(a2)
    l3 = create_list(a3)
    l4 = create_list(a4)
    p1 = partition(l1, 2)
    p2 = partition(l2, 2)
    p3 = partition(l3, 3)
    p4 = partition(l4, 1)
    print(restore_list(p1))
    print(restore_list(p2))
    print(restore_list(p3))
    print(restore_list(p4))
Exemple #5
0
def _print():
    a1 = [1, 2, 3, 4, 5, 6]
    l1 = create_list(a1)
    print(nth_to_last(l1, 0))
    print(nth_to_last(l1, 1).data)
    print(nth_to_last(l1, 3).data)
    print(nth_to_last(l1, 6).data)
    print(nth_to_last(l1, 7).data)
    nth_to_last2(l1, 4)
Exemple #6
0
def add_lists2(l1, l2):
    len1 = get_length(l1)
    len2 = get_length(l2)
    if len1 < len2:
        l1 = pad_list(l1, len2 - len1)
    else:
        l2 = pad_list(l2, len1 - len2)
    t = []
    carry = add_lists2_aux(l1, l2, t)
    res = create_list(t)
    if carry > 0:
        new_head = ListNode(1)
        new_head.next = res
        res = new_head
    return res
Exemple #7
0
def _test():
    a1 = [1]
    a2 = [1, 1]
    a3 = [1, 2]
    a4 = [1, 2, 1]
    a5 = [1, 2, 3, 1]
    a6 = [1, 3, 3, 1]
    d = locals()
    da = {
        'a1': True,
        'a2': True,
        'a3': False,
        'a4': True,
        'a5': False,
        'a6': True,
    }
    for a in d:
        l = create_list(d[a])
        assert is_palindrome(l) == da[a]
Exemple #8
0
def _print():
    a1 = [1, 2, 3, 4]
    l1 = create_list(a1)
    r1 = reverse_list(l1)
    print(restore_list(r1))
    n1 = [6, 1, 7]
    n2 = [2, 9, 5]
    nl1 = create_list(n1)
    nl2 = create_list(n2)
    r = add_lists2(nl1, nl2)
    print(restore_list(r))
    n3 = [1, 2, 3]
    n4 = [2, 3, 4]
    nl3 = create_list(n3)
    nl4 = create_list(n4)
    nl5 = create_list([9, 3, 4, 7])
    nl6 = create_list([9, 7, 2])
    rr = add_lists2(nl5, nl6)
    print(restore_list(rr))
    rrr1 = add_lists3(nl3, nl4)
    rrr2 = add_lists3(nl5, nl6)
    print(restore_list(rrr1))
    print(restore_list(rrr2))
Exemple #9
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()
Exemple #10
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()
Exemple #11
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()
Exemple #12
0
#!/bin/python
from linked_list import create_list, print_list, print_separator, N, get_random_middle, get_last

def detect_loop(root):
    node = root
    prev = None
    while not node.visited:
        node.visited = True
        prev = node
        node = node.next
    return prev

print "LOOP DETECTION"
root = create_list(N)
random_node = get_random_middle(root, N)
last_node = get_last(root)
last_node.next = random_node
last_loop_node = detect_loop(root)
print "Last Loop Node: {0}".format(last_loop_node.value)
print "First Loop Node: {0}".format(last_loop_node.next.value)
print "Rand Node: {0}".format(random_node.value)
last_node.next = None
print print_list(root)
print_separator()
Exemple #13
0
            p = p.next
        p.next = l1 or l2
        return dummy.next

    def mergeTwoLists2(self, l1: ListNode, l2: ListNode) -> ListNode:
        """
        Solution #2: recursively
        Time: O(n)
        Space: O(1)
        """
        if not l1: return l2
        if not l2: return l1
        if l1.val < l2.val:
            l1.next = self.mergeTwoLists2(l1.next, l2)
            return l1
        else:
            l2.next = self.mergeTwoLists2(l1, l2.next)
            return l2


solution = Solution()

l1 = create_list([1, 2, 3])
l2 = create_list([1, 2, 4])
ans1 = solution.mergeTwoLists1(l1, l2)
print_list(ans1)  # [1,1,2,2,3,4]

l1 = create_list([1, 2, 3])
l2 = create_list([3, 4, 5, 6, 7])
ans2 = solution.mergeTwoLists2(l1, l2)
print_list(ans2)  # [1, 2, 3, 3, 4, 5, 6, 7]
Exemple #14
0
def _print():
    a1 = [1, 2, 3, 4, 5]
    l1 = create_list(a1)
    node = l1.next.next  # 3
    delete_node(node)
    print(restore_list(l1))