Reverse

In this lesson, you will learn how to reverse a linked list in both an iterative and recursive manner.

In this lesson, we will look at how we can reverse a singly linked list in an iterative way and a recursive way.

Let’s first be clear about what we mean by reversing a linked list. Have a look at the illustration below to get a clear idea.

Let’s discuss the algorithm to reverse the linked list as depicted in the illustration above.

Algorithm #

Before jumping to the code, let’s figure out the algorithm.

Iterative Implementation #

If we look at the reversal above, the key idea is that we’re reversing the orientation of the arrows. For example, node A is initially pointing to node B but after we flip them, node B points to node A. The same is the case for other nodes. We can take this key observation to solve our ...