But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a free Interview Cake problem every week.
You're in!
Hooray! It's opposite day. Linked lists go the opposite way today.
Write a method for reversing a linked list. Do it in place.
Your method will have one input: the head of the list.
Your method should return the new head of the list.
Here's a sample linked list node class:
We can do this in space. So don't make a new list; use the existing list nodes!
We can do this is in time.
Careful—even the right approach will fail if done in the wrong order.
Try drawing a picture of a small linked list and running your method by hand. Does it actually work?
The most obvious edge cases are:
Does your method correctly handle those cases?
Our first thought might be to build our reversed list "from the beginning," starting with the head of the final reversed linked list.
The head of the reversed list will be the tail of the input list. To get to that node we'll have to walk through the whole list once ( time). And that's just to get started.
That seems inefficient. Can we reverse the list while making just one walk from head to tail of the input list?
We can reverse the list by changing the next pointer of each node. Where should each node's next pointer...point?
Each node's next pointer should point to the previous node.
How can we move each node's next pointer to its previous node in one pass from head to tail of our current list?
In one pass from head to tail of our input list, we point each node's next pointer to the previous item.
The order of operations is important here! We're careful to copy currentNode.next into next before setting currentNode.next to previousNode. Otherwise "stepping forward" at the end could actually mean stepping back to previousNode!
We return previousNode because when we exit the list, currentNode is null. Which means that the last node we visited—previousNode—was the tail of the original list, and thus the head of our reversed list.
time and space. We pass over the list only once, and maintain a constant number of variables in memory.
This in-place reversal destroys the input linked list. What if we wanted to keep a copy of the original linked list? Write a method for reversing a linked list out-of-place.
It's one of those problems where, even once you know the procedure, it's hard to write a bug-free solution. Drawing it out helps a lot. Write out a sample linked list and walk through your code by hand, step by step, running each operation on your sample input to see if the final output is what you expect. This is a great strategy for any coding interview question.
Reset editor
Powered by qualified.io