Exemplo n.º 1
0
def sum_lists_tt(ll1, ll2):
    ''' sums two linked lists
    ex:
        ll1 = 7 -> 1 -> 6   (617)
        ll2 = 5 -> 9 -> 2   (295)
        result = 617 + 295 = 912 =  2 -> 1 -> 9 
    '''
    result = Linked_List() 
    carry = 0

    if len(ll1) > len(ll2):
        big = ll1.head
        small = ll2.head
    else:
        big = ll2.head
        small = ll1.head

    while big:
        if small:
            num = big.data + small.data + carry
            small = small.next
        else:
            num = big.data + carry
            
        if num < 10: # no double digit, no carry
            carry = 0 
        else:
            carry = 1
            num %= 10 #get digit in one's place

        big = big.next
        result.add_back(num)

    return result
Exemplo n.º 2
0
def partition(ll, val):
    ''' changes linked list ll to have all values < val on left of all values >= val '''
    if ll.head is None:  # no nodes
        return False

    if ll.head.next is None:  # 1 node
        return False

    # create 2 linked lists that contain values >= or < val
    gte_ll = Linked_List()
    lt_ll = Linked_List()
    cur = ll.head

    while cur:
        if cur.data >= val:
            gte_ll.add_front(cur.data)
        else:
            lt_ll.add_front(cur.data)
        cur = cur.next

    # combine linked lists
    if lt_ll.tail is not None:
        lt_ll.tail.next = gte_ll.head
        ll.head = lt_ll.head
    return True
Exemplo n.º 3
0
def forward_sum_lists(ll1, ll2):
    ''' sums two linked lists
    ex:
        ll1 = 6 -> 1 -> 7   (617)
        ll2 = 2 -> 9 -> 5   (295)
        result = 617 + 295 = 912
        output = 9 -> 1 -> 2
    '''
    result = Linked_List() 
    carry = 0
    last = None 

    if len(ll1) > len(ll2):
        big = ll1
        small = ll2
    else:
        big = ll2
        small = ll1

    # 0 pad the small linked list to make same # nodes
    for _ in range ( len(big) - len(small) ):
        small.add_front(0)

    big = big.head
    small = small.head

    while big:
        val = big.data + small.data

        #no need to add a carry
        if val < 10 and last is not None:
            result.add_back(last)
            last = val
        elif last is not None: #val >= 10
            result.add_back(last + 1) #add the carry
            last = val % 10 
        else:
            last = val 

        big = big.next
        small = small.next

    return result
Exemplo n.º 4
0
        if cur.data >= val:
            gte_ll.add_front(cur.data)
        else:
            lt_ll.add_front(cur.data)
        cur = cur.next

    # combine linked lists
    if lt_ll.tail is not None:
        lt_ll.tail.next = gte_ll.head
        ll.head = lt_ll.head
    return True


if __name__ == "__main__":
    print '---------Test 1-------------'
    ll = Linked_List()
    ll.add_front(1)
    ll.add_back(2)
    ll.add_back(4)
    ll.add_back(9)
    ll.add_back(5)
    ll.add_back(3)
    ll.add_back(6)
    ll.add_back(2)
    ll.add_back(2)
    print ll
    print "calling partition, val = 4"
    print partition(ll, 4)
    print ll

    print '---------Test 2-------------'
Exemplo n.º 5
0
    for _ in range(k):
        if fast is None:
            return None  # out of bounds
        fast = fast.next

    while fast:
        slow = slow.next
        fast = fast.next

    return slow


if __name__ == "__main__":
    print '---------Test 1-------------'
    ll = Linked_List()
    ll.add_front(1)
    ll.add_back(2)
    ll.add_back(3)
    ll.add_back(4)
    ll.add_back(5)
    ll.add_back(6)
    print ll
    print "calling kth to last, k = 0 (invalid)"
    print kth_to_last(0, ll)
    print "calling kth to last, k = len(ll) (first)"
    print kth_to_last(len(ll), ll)
    print "calling kth to last, k = 1 (last)"
    print kth_to_last(1, ll)
    print "calling kth to last, k = 2 (second to last)"
    print kth_to_last(2, ll)
Exemplo n.º 6
0
    while cur:
        d[cur] = True
        cur = cur.next

    cur = ll2.head
    while cur:
        if cur in d:
            return cur
        cur = cur.next

    return None


if __name__ == "__main__":
    print "--------------Test 1--------------"
    ll1 = Linked_List()
    ll2 = Linked_List()

    a = Node('a')
    b = Node('b')
    c = Node('c')
    d = Node('d')
    e = Node('e')

    ll1.head = a
    a.next = b
    b.next = c
    c.next = e
    ll1.tail = e
    # a -> b -> c - > e
Exemplo n.º 7
0
 def test_alt_func(self):
     for [nodes, expected] in self.data:
         ll = Linked_List()
         ll.gen(*nodes)
         self.assertEqual(alt_palindrome(ll), expected)
Exemplo n.º 8
0
            last = val % 10 
        else:
            last = val 

        big = big.next
        small = small.next

    return result





if __name__ == "__main__":
    print '---------Test 1-------------'
    ll1 = Linked_List()
    ll1.add_back(7)
    ll1.add_back(1)
    ll1.add_back(6)
    ll2 = Linked_List()
    ll2.add_back(5)
    ll2.add_back(9)
    ll2.add_back(2)
    print ll1
    print ll2
    print "calling sum_lists()"
    print sum_lists(ll1, ll2)

    print '---------Test 2-------------'
    print 'One empty list'
    ll1 = Linked_List()
Exemplo n.º 9
0
    if ll.head is None:
        return

    slow = fast = ll.head
    while slow:
        while fast.next:
            if slow.data == fast.next.data:
                fast.next = fast.next.next
            else:
                fast = fast.next
        slow = fast = slow.next


if __name__ == "__main__":
    print '---------Test 1-------------'
    ll = Linked_List()
    ll.add_front(4)
    ll.add_back(5)
    ll.add_back(6)
    ll.add_back(6)
    ll.add_back(7)
    ll.add_back(6)
    print ll
    print "calling remove dups"
    remove_dups(ll)
    print ll
    print '---------Test 2-------------'
    ll = Linked_List()
    ll.add_front(4)
    ll.add_back(5)
    ll.add_back(6)