Grokking the Coding Interview: Patterns for Coding Questions
Vote
0% completed
Introduction to In-place Reversal of a Linked List Pattern
You are given the head of a linked list. Return the same list with its order reversed.
1 -> 2 -> 3 -> 4 -> null becomes 4 -> 3 -> 2 -> 1 -> null
The easy answer is to copy every value into an array, reverse the array, then write the values back into the nodes. It works, and it costs O(N) extra memory for a list you were already holding.
There is nothing to copy. A linked list is already made of the pieces you need. The only thing that makes the list run left to right is the direction of each next link. Turn each link around and the list runs the other way.
.....
.....
.....
Like the course? Get enrolled and start learning!
D
David Ng
· 4 years ago
This section should be before Section 5 (Fast and Slow Pointers), as it's needed to solve Problem Challenge 2 there.