Exemplo n.º 1
0
    def add_list(head1, head2):
        if head1 is None:
            return head2

        if head2 is None:
            return head1

        reversed_list1 = ListAddTool.revert_linked_list(head1)
        reversed_list2 = ListAddTool.revert_linked_list(head2)

        new_head = None
        new_list = None
        flag = 0

        while reversed_list1 is not None or reversed_list2 is not None:
            if reversed_list1 is None:
                value1 = 0
            else:
                value1 = reversed_list1.value

            if reversed_list2 is None:
                value2 = 0
            else:
                value2 = reversed_list2.value

            temp = value1 + value2 + flag
            if temp/10 >= 1:
                flag = 1
                if new_list is None:
                    new_head = Node(temp % 10)
                    new_list = new_head
                else:
                    new_list.next = Node(temp % 10)
                    new_list = new_list.next
            else:
                flag = 0
                if new_list is None:
                    new_head = Node(temp)
                    new_list = new_head
                else:
                    new_list.next = Node(temp)
                    new_list = new_list.next

            if reversed_list1 is not None:
                reversed_list1 = reversed_list1.next
            if reversed_list2 is not None:
                reversed_list2 = reversed_list2.next

        if flag == 1:
            new_list.next = Node(1)
        reversed_new_head = ListAddTool.revert_linked_list(new_head)
        return reversed_new_head
Exemplo n.º 2
0
    def insert_node_by_order(cls, head, num):
        node = Node(num)
        if head is None:
            node.next = node
            return node

        pre = None
        cur = head
        while True:
            if head.next == head:
                tail = head
                break
            if pre is not None and pre.value > cur.value:
                tail = pre
                break
            pre = cur
            cur = cur.next

        pre = None
        cur = head
        while True:
            if pre is not None and pre.value > cur.value:
                break
            if cur.value > node.value:
                if pre is not None:
                    pre.next = node
                    node.next = cur
                    return head
                else:
                    node.next = cur
                    tail.next = node
                    return node
            pre = cur
            cur = cur.next
        pre.next = node
        node.next = head
        return head
Exemplo n.º 3
0
            if cur1 is None:
                break

            while cur2 is not None and cur1.value > cur2.value:
                pre = cur2
                cur2 = cur2.next
            pre.next = cur1

            if cur2 is None:
                break

        if head1.value < head2.value:
            return head1
        else:
            return head2


if __name__ == '__main__':
    head1 = Node(0)
    head1.next = Node(2)
    head1.next.next = Node(3)
    head1.next.next.next = Node(7)

    head2 = Node(1)
    head2.next = Node(3)
    head2.next.next = Node(5)
    head2.next.next.next = Node(7)
    head2.next.next.next.next = Node(9)

    new_head = MergeListTool.merge(head1, head2)
    MergeListTool.print_list(new_head)
Exemplo n.º 4
0
            if reversed_list1 is not None:
                reversed_list1 = reversed_list1.next
            if reversed_list2 is not None:
                reversed_list2 = reversed_list2.next

        if flag == 1:
            new_list.next = Node(1)
        reversed_new_head = ListAddTool.revert_linked_list(new_head)
        return reversed_new_head

    @staticmethod
    def revert_linked_list(head):
        pre = None

        while head is not None:
            next = head.next
            head.next = pre
            pre = head
            head = next

        return pre

if __name__ == '__main__':
    node1 = Node(9)
    node1.next = Node(9)
    node1.next.next = Node(9)

    node2 = Node(1)

    ListAddTool.print_list(ListAddTool.add_list(node1, node2))
Exemplo n.º 5
0
                    if new_pre_node is not None and count == 1:
                        new_pre_node.next = temp_node
                    pre_node = temp_node
                    count -= 1
                new_pre_node = first_node
            cur = next
        if len(temp_stack) > 0:
            temp_node = temp_stack.pop(0)
            if new_pre_node is not None:
                new_pre_node.next = temp_node
            else:
                return temp_node

        return new_head


if __name__ == '__main__':
    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(4)
    head.next.next.next.next = Node(5)
    head.next.next.next.next.next = Node(6)
    head.next.next.next.next.next.next = Node(7)
    head.next.next.next.next.next.next.next = Node(8)
    k = 3
    ReversePartList.print_list(ReversePartList.reverse_part(head, k))



Exemplo n.º 6
0
                next = cur.next
                if fake_head.value == cur.value:
                    # del current
                    pre.next = next
                    flag = 1
                    break
                fake_head = fake_head.next
            if flag != 1:
                pre = cur
            cur = cur.next
            flag = 0
        return head


if __name__ == '__main__':
    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(3)
    head.next.next.next.next = Node(4)
    head.next.next.next.next.next = Node(4)
    head.next.next.next.next.next.next = Node(2)
    head.next.next.next.next.next.next.next = Node(1)
    head.next.next.next.next.next.next.next.next = Node(1)
    DuplicatedNodeRemove.print_list(DuplicatedNodeRemove.remove_duplicate_node(head))

    head = Node(1)
    head.next = Node(2)
    head.next.next = Node(3)
    head.next.next.next = Node(3)
    head.next.next.next.next = Node(4)
Exemplo n.º 7
0
            return head

        pre = None
        cur = head

        new_head = head

        while cur is not None:
            next = cur.next
            if cur.value == num:
                if pre is not None:
                    pre.next = next
                else:
                    new_head = cur.next
                    cur.next = None
                    del cur
            else:
                pre = cur
            cur = next

        return new_head


if __name__ == '__main__':
    node = Node(1)
    node.next = Node(2)
    node.next.next = Node(3)
    node.next.next.next = Node(2)
    node.next.next.next.next = Node(4)

    RemoveAssignedNode.print_list(RemoveAssignedNode.remove_node(node, 2))
Exemplo n.º 8
0
        if left_head is not None:
            if pivot_head is not None:
                left.next = pivot_head
                pivot_head.next = right_head
            else:
                left.next = right_head
            if right.next is not None:
                right.next = None
            return left_head

        else:
            if pivot_head is not None:
                pivot.next = right_head
                if right.next is not None:
                    right.next = None
                return pivot_head
            else:
                return right_head

if __name__ == '__main__':
    node = Node(9)
    node.next = Node(0)
    node.next.next = Node(4)
    node.next.next.next = Node(5)
    node.next.next.next.next = Node(1)

    partion_node = ListPartion.strict_partion(node, 4)
    ListPartion.print_list(partion_node)