from linkedlist import Node
def findKToLast(head, K):
    currentNode = head
    counter = 0
    kNode = head
    while currentNode is not None:
        if counter < K:
            counter += 1
        else:
            kNode = kNode.next
        currentNode = currentNode.next
        
    if counter == K:
        return kNode
    else:
        return None

testLinkedList = Node(1)
testLinkedList.createLinkedListFromList([2,3,4])
result = findKToLast(testLinkedList, 4)
print(result.data)

        

    while current_node is not None:
        new_node = Node(current_node.data)
        new_node.next = reversed_linked_list
        reversed_linked_list = new_node
        current_node = current_node.next

    current_node = head

    while current_node is not None and reversed_linked_list is not None:
        if current_node.data != reversed_linked_list.data:
            return False
        current_node = current_node.next
        reversed_linked_list = reversed_linked_list.next

    return True


print("============== TEST 1 ==================")
test_list1 = Node('a')
test_list1.createLinkedListFromList(['b', 'a'])
result1 = check_palindrome(test_list1)

print(result1)

print("============== TEST 2 ==================")
test_list2 = Node('a')
test_list2.createLinkedListFromList(['b', 'b'])
result2 = check_palindrome(test_list2)

print(result2)
    #extra while loop condition because it's possible that in the process of deletion we changed the next node to be none and
    # it naively changes it to none
    while currentNode is not None and currentNode.next is not None:
        searchNode = currentNode
        while searchNode.next is not None:
            if searchNode.next.data == currentNode.data:
                searchNode.next = searchNode.next.next
            else:
                searchNode = searchNode.next
        currentNode = currentNode.next


print("===========Test 1==========")
head = Node(4)
head.createLinkedListFromList([5, 6, 4])
head.printLinkedList()
removeDup(head)
print("Remove Duplicates")
head.printLinkedList()

print("===========Test 2==========")
head2 = Node(1)
head2.createLinkedListFromList([1, 1, 1, 1, 2, 2, 3, 3])
head2.printLinkedList()
print("Remove Duplicates")
removeDup(head2)
head2.printLinkedList()

print("===========Test 3==========")
head3 = Node(4)
#much better version
#remember that deleting a node is just rearranging its next pointer to skip the one we want to delete
#also I don't have to shift the whole linked list
#finally and most importantly, look at the linked list as a whole. Realize that what we were trying to achieve didn't require shifting the whole array
# ex. 1->2->3->4->5 and we want to delete 3 then we would get 1->2->4->5
# from this example we should realize that the 4 is in the old place of the three!
def better_del_middle_node(mNode):
    if mNode is None or mNode.data == None:
        return False

    mNode.data = mNode.next.data
    mNode.next = mNode.next.next
    return True


testLinkedlist = Node(1)
testLinkedlist.createLinkedListFromList([2, 3, 4, 5])
targetNode = getNode(testLinkedlist, 3)

better_del_middle_node(targetNode)

testLinkedlist.printLinkedList()

testLinkedlist2 = Node(1)
testLinkedlist2.createLinkedListFromList([2, 3, 9, 5])
targetNode2 = getNode(testLinkedlist2, 9)

better_del_middle_node(targetNode2)

testLinkedlist2.printLinkedList()
Ejemplo n.º 5
0
    while current_node is not None:
        left_partition, right_partition = assign_to_partition(
            left_partition, right_partition, partition, current_node)
        if left_head is None and left_partition is not None:
            left_head = left_partition
        if right_head is None and right_partition is not None:
            right_head = right_partition
        current_node = current_node.next

    #merge the two partition halves«»
    #We want to attach it to the head of the right partition!
    left_partition.next = right_head

    if right_partition is not None:
        right_partition.next = None

    return left_head


testLinkedList = Node(1)
testLinkedList.createLinkedListFromList([2, 7, 3, 9])
result_linked_list = partition_linked_list(testLinkedList, 5)
result_linked_list.printLinkedList()

#book test
testLinkedList2 = Node(3)
testLinkedList2.createLinkedListFromList([5, 8, 5, 10, 2, 1])
result_linked_list2 = partition_linked_list(testLinkedList2, 5)
result_linked_list2.printLinkedList()
            p2 = p2.next

    if carry == 1:
        new_linked_list.next = Node(1)

    return head

print("============= TEST 1 ===============")
num1 = Node(1)
num2 = Node(2)
result1 = add_linked_list(num1, num2)
result1.printLinkedList()

print("============= TEST 2 ===============")
num3 = Node(7)
num3.createLinkedListFromList([1,6])
num4 = Node(5)
num4.createLinkedListFromList([9,2])
result2 = add_linked_list(num3, num4)
result2.printLinkedList()

print("============= TEST 3 ===============")
num5 = Node(5)
num5.createLinkedListFromList([6,7])
num6 = Node(4)
result3 = add_linked_list(num5, num6)
result3.printLinkedList()

print("============= TEST 4 ===============")
num7 = Node(0)
num7.createLinkedListFromList([0,1])