I'm trying to combine two linked lists iteratively, but what I have right now is giving me the reverse of the result. Is there a way to combine the lists in the correct order without having to reverse if after the result list is created?
class Link:
empty = ()
def __init__(self, first, rest=empty):
assert rest is Link.empty or isinstance(rest, Link)
self.first = first
self.rest = rest
def __add__(self, lst):
"""
>>>s = Link(1, Link(2))
>>>s + Link(3,Link(4))
Link(1,Link(2,Link(3,Link(4))))
"""
result = Link.empty
while self is not Link.empty:
result = Link(self.first,result)
self = self.rest
while lst is not Link.empty:
result = Link(lst.first, result)
lst = lst.rest
return result