def add(left, right, msd=False): """Set msd to True if left and right are in "most significant digit first" order. The result will be in the same order as the input.""" if msd: left = left.reverse() right = right.reverse() result = SinglyLinkedList() carry = 0 a = left.head b = right.head while a and b: c = a.payload + b.payload result.append(carry + c % 10) carry = c / 10 a = a.next_ b = b.next_ while a: result.append(a.payload) a = a.next_ while b: result.append(b.payload) b = b.next_ if msd: return result.reverse() return result
def test_sanity(self): a = range(10) sll = SinglyLinkedList(a) self.assertEquals(list(sll.reverse()), [x for x in reversed(a)])
def test_sanity(self): a = range(10) sll = SinglyLinkedList(a) self.assertEquals(list(sll.reverse()), [x for x in reversed(a)])