|
|
|
|
|
by wolfgke
4270 days ago
|
|
No need for pointer manipulation. When I had to implement reversing a linked list (for which already an implementation is there) I'd first ask whether it's single or double linked. In the former case (Pseudocode) LinkedList toInvert;
LinkedList out;
Either iterate through toInvert and call out.push_front(...) on each element or (if the operation may be destructive) do while (!toInvert.empty)
out.push_front(toInvert.pop_front())
All this should not take more than 5-6 lines - should be doable in 20 minutes. Also: these two lines surely could also be written under pressure.If the linked list is double linked, I'd ask whether the reversing has to be called often in the code. If yes, you could add a direction flag to the linked list, which tells in which direction the iterator will iterate. This makes the iterator implementation a little bit more complicated, but makes the reversing O(1). |
|