Reverse a singly linked list

Reverse a singly linked list

Non-recursive method:
For this, we will use there pointer, namely previousNodePtr, currentNodePtr & nextNodePtr as shown in image.

And update their values in loop.

Recursive method:
We call recursive method continuously until we reach at end of list and after we update next pointer. Here is method call stack,


List: [1, 2, 3, 4, ]
List after reverse: [4, 3, 2, 1, ]
List after reverse (recursive):[1, 2, 3, 4, ]

Author: Hrishikesh Mishra