示例#1
0
def linked_list_add_forward(L1, L2, carry):
    # compare length of linked lists and pad the shorter one with 0
    l1_len = len_list(L1)
    l2_len = len_list(L2)
    if l1_len < l2_len:
        L1 = padInFront(L1, l2_len - l1_len)
    else:
        L2 = padInFront(L2, l1_len - l2_len)
    # Add lists
    sumandcarry = addListsFwd2Helper(L1, L2)
    #If there was a carry value left over, insert this at the front of the lit
    #Otherwise, just return the linked list
    result = LinkedList()
    result.first = sumandcarry[0]   
    # If the carry is not 0, insert this at the front of the linked list 
    if sumandcarry[1] == 0:
        return result
    else:
        result = insertBefore(result, sumandcarry[1])
        return result
示例#2
0
    if node == None:
        return None
    fast = node
    slow = node
    firsthalf = []
    while fast != None and fast.next != None:
        firsthalf.append(slow.value)
        slow = slow.next
        fast = fast.next.next
    if fast != None:
        slow = slow.next
    while slow != None:
        if firsthalf.pop() != slow.value:
            return False
        else:
            slow = slow.next
    return True


L1 = randomLinkedList(3, 3, 4)
print(L1)
print(is_palindrome(L1.first))

L2 = LinkedList()
for i in range(1,4):
    L2.addNode(i)
for i in range(3, 0, -1):
    L2.addNode(i)
print(L2)
print(is_palindrome(L2.first))
示例#3
0
        fast = fast.next.next
        if (slow == fast) : 
            break # collision
        
        #error checking - no meeting point and therefore no loop
    if (fast == None or fast.next == None):
        return None
        
    #move slow to head. Keep fast at meeting point. 
    #each are k steps from the loop start. If they move at same pace,
    #they meet at loop start
    slow = head
    while(slow != fast):
        slow = slow.next
        fast = fast.next
        
    #both now point to the start of loop
    return fast

loop = LinkedListNode("C")
loop.next = LinkedListNode("D")
loop.next.next = LinkedListNode("E")
loop.next.next.next = loop
L1 = LinkedList()
L1.addReadyNode(loop)
L1.addNode("B")
L1.addNode("T")
L1.addNode("R")
L1.addNode("A")

print("start loop is :"+str(find_begining(L1.first)))
示例#4
0
def padInFront(linkedlist, padding):
    head = linkedlist
    for i in range(padding):
        insertBefore(head, 0)
    return head

# Helper function to calculate length of a linked list
def len_list(linkedlist):
    length = 0
    current = linkedlist
    while current != None:
        length += 1
        current = current.next
    return length

first_number = LinkedList()
first_number.addNode(6)
first_number.addNode(1)
first_number.addNode(7)

second_number = LinkedList()
second_number.addNode(2)
second_number.addNode(9)
second_number.addNode(5)

print("First number: " + str(first_number))
print("           +    ")
print("Second number: " + str(second_number))
print("           =    ")
print(linked_list_add(first_number.first, second_number.first, 0).print_list())
print("===================================================")