예제 #1
0
def solver(input):
    if input == None:
        return False

    # just swap the value of the current node with the value of the next node
    next = input.next
    input.value = next.value
    input.next = next.next

    return True


if __name__ == "__main__":
    values = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    ll = LinkedList()
    ll2 = LinkedList()
    for x in values:
        ll.addToTail(x)
        if x != 5:
            ll2.addToTail(x)

    curr = ll.head
    while curr.value != 5:
        curr = curr.next

    # now that we have 5, pass that value into the function
    solver(curr)

    ll.printList()
    ll2.printList()
예제 #2
0
            p1 = p1.next
            p2 = p2.next

    # p2 should be at the end, but if the current value there has been seen, then we have to remove this node
    if p2.value not in seen:
        p1.next = None

    # return the new linked list
    return input


if __name__ == "__main__":
    # Create unsorted Linked list
    l1 = LinkedList()
    toAdd = [5, 6, 5, 1, 2, 1, 2]
    for x in toAdd:
        l1.addToTail(x)

    # do the problem
    print("========== RESULT ==========")
    solver(l1)
    l1.printList()

    # Create unsorted Linked list
    print("========== ANSWER ==========")
    s1 = LinkedList()
    ans = [5, 6, 1, 2]
    for x in ans:
        s1.addToTail(x)
    s1.printList()