示例#1
0

def removeNthFromEnd(head, n):
    temp = adt.ListNode(0)
    temp.next = head

    # setup window
    curr = end = temp
    for _ in range(n):
        end = end.next

    # move end to last elem of list so curr.next points to deletion node
    while end.next:
        curr = curr.next
        end = end.next

    # pointer manipulation
    curr.next = curr.next.next
    return curr.next if curr == temp else head


if __name__ == "__main__":
    print("Enter space-separated integers as list: ", end="")
    nums = adt.array_to_list(list(map(int, input().strip().split(' '))))

    print("Enter n: ", end="")
    n = int(input().strip())

    print("Original list: {}".format(nums))
    print("Modified list: {}".format(removeNthFromEnd(nums, n)))
示例#2
0
    while curr:
        if curr.val in candidates:
            linked = True
        else:
            # increment count and reset flag
            # if previous elements were linked
            if linked:
                count += 1
                linked = False

        curr = curr.next

    # handle edge case where the list ends on a linked component
    if linked: count += 1
    return count


if __name__ == "__main__":
    print("Enter space-separated numbers: ", end="")
    nums = list(map(int, input().strip().split(' ')))

    print("Enter space-separated candidates: ", end="")
    G = list(map(int, input().strip().split(' ')))

    head = array_to_list(nums)
    print(head)

    count = numComponents(head, G)
    print("There {} {} connected component{}.".format(
        "is" if count == 1 else "are", count, "" if count == 1 else "s"))
示例#3
0
    # compute index of new head
    new_head_idx = length - (k % length)
    if new_head_idx == length:
        # no need to rotate
        return head

    # new_head.prev becomes the tail
    nodes[new_head_idx - 1].next = None

    # previous tail now points to head
    nodes[length - 1].next = head

    return nodes[new_head_idx]


if __name__ == "__main__":
    print("Enter space-separated integers for original list: ", end="")
    nums = list(map(int, input().strip().split(' ')))

    print("Enter rotation integer: ", end="")
    k = int(input().strip())

    nums = array_to_list(nums)
    print("Original list:")
    print(nums)

    nums = rotateRight(nums, k)
    print("Rotated list:")
    print(nums)
示例#4
0
    prev = dummy
    while prev.next:
        # set up window of same values
        end = curr = prev.next
        val = curr.val

        # move pointer until end or values differ
        while end.next and end.next.val == val:
            end = end.next

        if curr == end:
            # no deletion - move to next
            prev = curr
        else:
            # delete [curr, end]
            prev.next = end.next

    return dummy.next


if __name__ == "__main__":
    print("Enter space-separated numbers: ", end="")
    nums = sorted(list(map(int, input().strip().split(' '))))

    head = adt.array_to_list(nums)
    print(head)

    head = deleteDuplicates(head)
    print(head)
示例#5
0
        # advance right pointer
        right = right.next
        if right.next is None:
            break
        right = right.next

    # partition w.r.t. midpoint defined by left
    prev.next = None

    # recursively build children
    leftChild = sortedListToBST(head)
    rightChild = sortedListToBST(left.next)

    # create, setup and return root node
    root = TreeNode(left.val)
    root.left = leftChild
    root.right = rightChild
    return root


if __name__ == "__main__":
    head = array_to_list([-10, -3, 0, 5, 9])
    print("Linked list:")
    print(head)
    print()

    root = sortedListToBST(head)
    print("BST:")
    print(root)