Search code examples
algorithmsortingdata-structureslinked-listtraversal

Why are Linked Lists Reversed?


Why are Linked Lists Reversed?

I have been listening to a lot of Software Engineers that talk about reversing Linked Lists. What is the use of reversing a linked list? What are the advantages of doing so rather than traversing backwards?

  • Why are they used in technical interviews?
  • What is the use besides technical interviews?

Solution

  • Reversing a linked list comes up every now and then in real life.

    Most recently I did that while implementing a lock-free multi-producer single-consumer work queue. Stacks are the simplest lock-free data structure to implement, and the implementation is a singly-linked list, so my multiple producers would push new work onto a lock free stack.

    The single consumer can then get the whole stack at once, but then has to reverse it to make a queue of tasks in the correct order.

    That said... you're probably asking the wrong question. The task of reversing a linked list is one of the easiest data structure manipulations you can do. If you're being taught to do it, then it's just an early lesson on the long road to really understanding programming. If you're being asked about it in an interview, then the interviewer wants to know if you might be programmer who can design, implement, and maintain data structures, or if you can just copy and modify answers from StackOverflow, and glue together things you don't understand.