def runner():
    ll = LinkedList()
    for i in range(1, 10, 2):
        ll.appendToTail(i)
    for i in range(2, 11, 2):
        ll.appendToTail(i)

    print('Before - ', ll)
    p1 = p2 = ll.__get__()

    while p1.next is not None and p1.next.next is not None:
        p1 = p1.next.next
        p2 = p2.next

    p2 = p2.next
    p1 = ll.__get__()

    while p2.next is not None:
        temp = p1.next
        p1.next = p2
        p1 = temp

        temp = p2.next
        p2.next = p1
        p2 = temp

    p1.next = p2
    print('After - ', ll)
Exemplo n.º 2
0
        "input1": [7, 1, 6],
        "input2": [5, 9, 2],
        "output": [1, 3, 0, 8]
    }, {
        "id": 3,
        "input1": [1, 2, 3, 4],
        "input2": [9, 6, 3],
        "output": [2, 1, 9, 7]
    }, {
        "id": 4,
        "input1": [5, 6],
        "input2": [1, 2, 3, 4],
        "output": [1, 2, 9, 0]
    })

    for testCase in TEST_CASES:
        ll1 = LinkedList()
        ll2 = LinkedList()
        for i in testCase["input1"]:
            ll1.appendToTail(i)
        for i in testCase["input2"]:
            ll2.appendToTail(i)

        head = sumList(ll1.__get__(), ll2.__get__())
        linkList = LinkedList(head)

        if linkList == testCase["output"]:
            print(f'TEST #{testCase["id"]} PASSED')
        else:
            print(f'TEST #{testCase["id"]} FAILED')
Exemplo n.º 3
0
#!/usr/bin/python3
"""
Traverse LinkedList Recursively
"""
from LinkedList import LinkedList, Node


def printASC(node: Node) -> None:
    if node is None:
        print('NULL', end='\n')
        return

    print(f'[{node}]', end='~>')
    printASC(node.next)


def printDESC(node: Node, stack: int) -> None:
    if node is None:
        return

    printDESC(node.next, stack + 1)
    print(f'[{node}]', end='~>NULL' if stack == 0 else '~>')


if __name__ == "__main__":
    ll = LinkedList()
    for i in range(1, 11):
        ll.appendToTail(i)

    printASC(ll.__get__())
    printDESC(ll.__get__(), 0)
Exemplo n.º 4
0
            head = node
        else:
            tail.next = node
            tail = node
        node = next_node
    tail.next = None
    return head


if __name__ == "__main__":
    TEST_CASES = ({
        "id": 1,
        "input": [3, 5, 8, 5, 10, 2, 1],
        "x": 5,
        "output": [1, 2, 3, 5, 8, 5, 10]
    }, )

    for testCase in TEST_CASES:
        ll = LinkedList()
        for i in testCase["input"]:
            ll.appendToTail(i)

        final_head = partition(ll.__get__(), testCase["x"])
        ll.__set__(final_head)
        print(ll)

        if ll == testCase["output"]:
            print(f'TEST #{testCase["id"]} PASSED')
        else:
            print(f'TEST #{testCase["id"]} FAILED')
Exemplo n.º 5
0
    TEST_CASES = ({
        "id": 1,
        "input": [4, 5, 9, 2, 4, 3, 9],
        "output": [4, 5, 9, 2, 3]
    }, {
        "id": 2,
        "input": [1, 0, 1, 0],
        "output": [1, 0]
    }, {
        "id": 3,
        "input": [5, 5, 5],
        "output": [5]
    }, {
        "id": 4,
        "input": [1, 2, 3, 4, 5],
        "output": [1, 2, 3, 4, 5]
    })

    for testCase in TEST_CASES:
        ll = LinkedList()
        for val in testCase["input"]:
            ll.appendToTail(val)

        removeDuplicate(ll.__get__())
        # deleteDuplicate(ll.__get__())

        if ll == testCase["output"]:
            print(f'TEST #{testCase["id"]} PASSED')
        else:
            print(f'TEST #{testCase["id"]} FAILED')
Exemplo n.º 6
0
        "id": 2,
        "input1": [6, 1, 7],
        "input2": [2, 9, 5],
        "output": [8, 0, 3, 1]
    }, {
        "id": 3,
        "input1": [4, 3, 2, 1],
        "input2": [3, 6, 9],
        "output": [7, 9, 1, 2]
    }, {
        "id": 4,
        "input1": [6, 5],
        "input2": [4, 3, 2, 1],
        "output": [0, 9, 2, 1]
    })

    for testCase in TEST_CASES:
        ll1 = LinkedList()
        ll2 = LinkedList()
        for val in testCase["input1"]:
            ll1.appendToTail(val)
        for val in testCase["input2"]:
            ll2.appendToTail(val)

        linkList = sumList(ll1.__get__(), ll2.__get__())

        if linkList == testCase["output"]:
            print(f'TEST #{testCase["id"]} PASSED')
        else:
            print(f'TEST #{testCase["id"]} FAILED')
Exemplo n.º 7
0
        "input1": [3, 1, 5, 9, 7, 2, 1],
        "input2": [4, 6, 7, 2, 1],
        "common": None
    }]

    for testCase in TEST_CASES:
        list1 = LinkedList()
        for i in testCase["input1"]:
            list1.appendToTail(i)

        list2 = LinkedList()
        for i in testCase["input2"]:
            list2.appendToTail(i)

        commonList = None
        if testCase["common"] is not None:
            commonList = LinkedList()
            for i in testCase["common"]:
                commonList.appendToTail(i)
            list1.appendNodeToTail(commonList.__get__())
            list2.appendNodeToTail(commonList.__get__())

        joiningNode = findIntersection(list1.__get__(), list2.__get__())

        if (joiningNode is None
                and commonList is None) or (id(joiningNode) == id(
                    commonList.__get__())):
            print(f'TEST #{testCase["id"]} PASSED')
        else:
            print(f'TEST #{testCase["id"]} FAILED')
Exemplo n.º 8
0
if __name__ == "__main__":
    TEST_CASES = ({
        "id": 1,
        "input": [1, 2, 3, 4],
        "k": 2,
        "output": 3
    }, {
        "id": 2,
        "input": [1, 2, 3, 4],
        "k": 1,
        "output": 4
    }, {
        "id": 3,
        "input": [1, 2, 3, 4],
        "k": 5,
        "output": None
    })

    for testCase in TEST_CASES:
        ll = LinkedList()
        for val in testCase["input"]:
            ll.appendToTail(val)

        o = kthLast(ll.__get__(), testCase["k"])

        if o["data"] == testCase["output"]:
            print(f'TEST #{testCase["id"]} PASSED')
        else:
            print(f'TEST #{testCase["id"]} FAILED')