def test_append(self): a = range(10) b = range(11) sll = SinglyLinkedList(a) self.assertEquals(list(sll), a) sll.append(10) self.assertEquals(list(sll), b)
def partition(sll, x): lt = SinglyLinkedList() geq = SinglyLinkedList() for value in sll: if value < x: lt.append(value) else: geq.append(value) return lt + geq
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_append_empty(self): sll = SinglyLinkedList() self.assertEquals(list(sll), []) sll.append(1) self.assertEquals(list(sll), [1])