def sumLists(lst1, lst2): ''' find sum of the input lists and output as a list ''' curr1, curr2 = lst1.head, lst2.head result = LinkedList() carry = 0 while curr1 is not None and curr2 is not None: res = curr1.data + curr2.data + carry carry = res / 10 res = res % 10 result.add(Node(res)) curr1 = curr1.next curr2 = curr2.next rem = curr1 if curr2 is None else curr2 while rem is not None: res = rem.data + carry carry = 0 if res > 9: res = res - 10 carry = 1 result.add(Node(res)) rem = rem.next return result
def test_isPalindrome(): ''' test for isPalindrome methods ''' lst = LinkedList() lst.add(1).add(2).add(3) assert not isPalindrome(lst) assert not isPalin2(lst.head, lst.head)[0] lst.add(2).add(1) assert isPalindrome(lst) assert isPalin2(lst.head, lst.head)[0] print 'Test Passed.'
def test_hasLoop(): ''' test for hasLoop method ''' print 'Testing hasLoop method ... ', nodea = Node('a') lst = LinkedList() ignore = [lst.add(Node(x)) for x in range(4)] lst.add(nodea) ignore = [lst.add(Node(x)) for x in range(10)] assert not hasLoop(lst)[0] lst.tail.next = nodea result, node = hasLoop(lst) assert result assert node is nodea print 'Passed'
def add(self, item): node = Node(item) LinkedList.add(self, node)