Refer to assignment for description of problem.
Solution:
Here are two solutions, the first the one I had mind, and the second also using pointers, in combination with recursion. (The recursive version is probably not very efficient, but I think it's kind of pretty. It was inspired by a student solution some years ago; I hadn't thought of using recursion!)