Example #1
0
def addLists_fwd_2(L1, L2):
    # compare length of linked lists and pad the shorter one with 0
    l1_len = lengthOfLinkedlist(L1)
    l2_len = lengthOfLinkedlist(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.head, L2.head)
    result = LinkedList()
    result.head = sumandcarry[0]
    # If the carry is not 0, insert this at the front of the linked list
    if sumandcarry[1] != 0:
        addNodeInFront(result, sumandcarry[1])
    return result
def addLists_fwd_2(L1, L2):
    # compare length of linked lists and pad the shorter one with 0
    l1_len = lengthOfLinkedlist(L1)
    l2_len = lengthOfLinkedlist(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.head, L2.head)
    result = LinkedList()
    result.head = sumandcarry[0]   
    # If the carry is not 0, insert this at the front of the linked list 
    if sumandcarry[1] != 0:
        addNodeInFront(result, sumandcarry[1])
    return result
Example #3
0
def padInFront(linkedlist, number):
    padlist = LinkedList()
    padlist.head = linkedlist.head
    for i in range(number):
        addNodeInFront(padlist, 0)
    return padlist
def padInFront(linkedlist, number):
    padlist = LinkedList()
    padlist.head = linkedlist.head
    for i in range(number):
        addNodeInFront(padlist, 0)
    return padlist