コード例 #1
0
ファイル: ispalindrome.py プロジェクト: bzelditch/EPI
# Unfortunately, we can't iterate backwards through the list b/c it's singly linked.....


def is_palindrome(start):

    contents = []
    current = start
    while current != None:
        contents.append(current.getData())
        current = current.getNext()

    if len(contents) == 0 or len(contents) == 1:
        return True

    midpoint = int(math.floor(len(contents) / 2))
    for i in range(midpoint):
        if contents[i] != contents[len(contents) - 1 - i]:
            return False

    return True


root = Node(1)
root.addNext(Node(2))
root.addNext(Node(1))
root.addNext(Node(4))
root.addNext(Node(2))
root.addNext(Node(1))

print(is_palindrome(root))
コード例 #2
0
        pointer1 = pointer1.getNext()
        pointer2 = pointer2.getNext()
        exponent += 1

    # At this point, either pointer1 = None or pointer2 = None or both equal None
    # If both, we can just return the sum
    if pointer1 == None and pointer2 == None:
        return list_sum

    elif pointer1 == None:
        while pointer2 != None:
            power = 10**exponent
            list_sum += pointer2.getData() * power
            pointer2 = pointer2.getNext()
            exponent += 1

    else:
        while pointer1 != None:
            power = 10**exponent
            list_sum += pointer1.getData() * power
            pointer1 = pointer1.getNext()
            exponent += 1

    return list_sum


root1 = Node(14)
root1.addNext(Node(1))
root2 = Node(15)

print(sum_lists(root1, root2))
コード例 #3
0
ファイル: detectcycle.py プロジェクト: bzelditch/EPI
# Detect the *first* node in a cycle in a circular linkedlist.

# BFS is an obvious approach, but we can try something else

def detect_cycle(start):

	# Idea: 1) We can maintain a list of nodes that we've seen and return the first node seen twice
	#       2) We can have two pointers run through the list, one twice the rate of the other.
	#          If, at any point, the two pointers point to the same node, that's the one

	pointer1 = start
	pointer2 = start
	check = 0
	while pointer1 != pointer2 or check == 0: # They both start at the same node
		check = 1
		pointer1 = pointer1.getNext()
		pointer2 = pointer2.getNext().getNext() # Moves twice as fast

	return pointer1.getData()


root = Node(1)
root.addNext(Node(2))
root.addNext(Node(3))
begin = Node(4) # Where the cycle starts
root.addNext(begin)
root.addNext(Node(5))
root.addNext(Node(6))
root.addNext(begin)

print(detect_cycle(root))
コード例 #4
0
    while current != None:
        val = current.getData()
        if val in seen:
            # Remove this node from the list
            next_node = current.getNext()
            previous.setNext(next_node)

        else:
            seen.append(val)
            previous = current

        current = current.getNext()

    return start_node  # We can't return "current".....


root = Node(14)
root.addNext(Node(1))
root.addNext(Node(13))
root.addNext(Node(14))
root.addNext(Node(2))
root.addNext(Node(3))
root.addNext(Node(5))
root.addNext(Node(11))
root.addNext(Node(25))

print(root.toStr())

mod = remove_dups1(root)
print(mod.toStr())