Neato.<p>If reference locality is a big concern and k is small, the following might perform better: allocate a ring buffer of size k+1 (ideally on the stack), enqueue the pointers as you go, when you hit the end, return the tail of the buffer.<p>Among other things, this problem well illustrates how the rules of optimization can vary wildly depending on low level architecture.