Write a function in Python that mutates a linked list,
lnk1, so that another linked list
lnk2 is appended onto the end of it.
def append_link(lnk1, lnk2): """ >>> lnk = Link(1, Link(2)) >>> append_link(lnk, Link(3, Link(4))) >>> lnk Link(1, Link(2, Link(3, Link(4)))) """ # Your Code Here
def append_link(lnk1, lnk2): if lnk1.rest is Link.empty: lnk1.rest = lnk2 append_link(lnk1.rest, lnk2)
For this particular problem, what we want to do is traverse
lnk1 until we get to the end, and then attach
lnk2 onto that somehow. That's how we get the recursive case - if we haven't gotten to the end of
lnk1 yet, we should recurse to make our way down the Linked list.
Once we're there though, we actually want to append
lnk2. We can do this by setting
lnk1.rest (which is
Link.empty by this point) to