def rearrange(l, data): if not l.head: return False # append an extra node at the front to handle the special case # of head greater than data tmpList = LinkedList() tmp = l.head while tmp: if tmp.data < data: # delete tmp.next # tmptmp = tmp.next # tmp.next = tmp.next.next # insert the deleted node at begining tmpList.insert_first(Node(tmp.data)) if tmp.data >= data: # delete tmp.next # tmptmp = tmp.next # tmp.next = tmp.next.next # insert the deleted node at end tmpList.insert_last(Node(tmp.data)) tmp = tmp.next return tmpList
def main(): l1 = LinkedList() l2 = LinkedList() [l1.insert_first(Node(0)) for i in range(0, 2)] [l2.insert_first(Node(1)) for i in range(0, 3)] print l1 print l2 print add_numbers(l1, l2)