コード例 #1
0
ファイル: q7.py プロジェクト: gzdshn/ctci-python
def getReversedList(head_in):
    list_new = LinkedList()
    curr = head_in
    while curr:
        list_new.insert(curr.val, 0)
        curr = curr.next
    return list_new.getHeadNode()
コード例 #2
0
## Tests For Singly Linked List Implementation
from myllist import LinkedList

list1 = LinkedList()
print "Size is: " + str(list1.size())

list1.append(1)
list1.append(2)
list1.append(3)
print "Size is: " + str(list1.size())
for idx in range(list1.size()):
    print list1.get(idx)

list1.insert(4, 3)
print "Size is: " + str(list1.size())
for idx in range(list1.size()):
    print list1.get(idx)

list1.remove(3)
print "Size is: " + str(list1.size())
for idx in range(list1.size()):
    print list1.get(idx)
コード例 #3
0
ファイル: q5.py プロジェクト: gzdshn/ctci-python
        return head1.val * multiplier + head2.val * multiplier + sumForwardHelper(
            head1.next, head2.next, multiplier / 10)


def findMaxMultiplier(head1, head2):
    length1 = 0
    curr = head1
    while curr:
        length1 += 1
        curr = curr.next
    length2 = 0
    curr = head2
    while curr:
        length2 += 1
        curr = curr.next
    return pow(10, max(length1, length2) - 1)


list1 = LinkedList()
for n in [7, 1, 6]:
    list1.append(n)

list2 = LinkedList()
for n in [5, 9, 2]:
    list2.append(n)

headNode1 = list1.getHeadNode()
headNode2 = list2.getHeadNode()
print sumReverse(headNode1, headNode2)
print sumForward(headNode1, headNode2)