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 in reverse order.

1 -> 2 -> 3 -> 4 -> null

becomes

4 -> 3 -> 2 -> 1 -> null

A simple solution copies every value into an array. It reverses the array and writes the values back into the nodes.

This works, but it needs O(N) extra space.

We do not need to move or copy the values. A linked list gets its order from the next reference in each node.

If we reverse every next reference, the list points in the opposite direction.

There is one important problem. After changing `current.next

.....

.....

.....

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.

Reading Progress

0%


Vote for new content