Пример #1
0
        elif flag is True:
            new_node.set_next(Node(first.get_data()))
            new_node = new_node.get_next()
            first = first.get_next()
            flag = False

        elif flag is False:
            new_node.set_next(Node(secound.get_data()))
            new_node = new_node.get_next()
            secound = secound.get_next()
            flag = True

    if secound is not None:
        new_node.set_next(secound)
    if first is not None:
        new_node.set_next(first)

    return new_list.head

if __name__ == '__main__':
    l1 = LinkedList()

    l1.create_list([40, 35, 30, 25, 20, 15, 10])

    out = rearrange(l1.head)

    while out is not None:
        print(out.get_data(), end=' ')
        out = out.get_next()
Пример #2
0
__author__ = '__naresh__'

## WAP to print list from the END

"""
------------ Algo ------------

Simple Just use Recursion. Once End reach start printing data

"""

from single_linked_list import LinkedList


def print_list_from_end(head):
    if head is None:
        return
    print_list_from_end(head.get_next())
    print head.get_data()


if __name__ == "__main__":
    l1 = LinkedList()
    l1.create_list([40, 35, 30, 25, 20, 15, 10, 5, 1, 2, 75, 85])

    print_list_from_end(l1.head)
Пример #3
0
    main_node = Node(0)
    node = main_node

    while l1 is not None and l2 is not None:
        if l2.get_data() <= l1.get_data():
            node.set_next(Node(l1.get_data()))
            node = node.get_next()
            l1 = l1.get_next()
        elif l1.get_data() <= l2.get_data():
            node.set_next(Node(l2.get_data()))
            node = node.get_next()
            l2 = l2.get_next()
    if l1 is None:
        node.set_next(l2)
    else:
        node.set_next(l1)
    return main_node.get_next()

if __name__ == '__main__':
    l1 = LinkedList()
    l2 = LinkedList()

    l1.create_list([40, 30, 15, 5])
    l2.create_list([45, 40, 10])

    out = merge(l1.head, l2.head)

    while out is not None:
        print(out.get_data(), end=' ')
        out = out.get_next()