7) Reverse a Linked List – Iterative and Recursive approach

This is one of the most important interview question, How to reverse a linked list using iterative and recursive approach? In this article, I will help you to understand, how to approach and solve this problem. I’m going to make a diagrammatic representations, so that you can easily understand the problem well.

Here is our given problem, Reverse a singly linked list.


Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

Diagrammatic explanations for Iterative Approach

Diagrammatic explanations for Recursive Approach


public ListNode reverseList(ListNode head) {
    /* iterative solution */
    ListNode newHead = null;
    while (head != null) {
        ListNode next = head.next;
        head.next = newHead;
        newHead = head;
        head = next;
    return newHead;
public ListNode reverseList(ListNode head) {
    /* recursive solution */
    return reverseListInt(head, null);
private ListNode reverseListInt(ListNode head, ListNode newHead) {
    if (head == null)
        return newHead;
    ListNode next = head.next;
    head.next = newHead;
    return reverseListInt(next, head);

Time Complexity: O(n)

Space Complexity: O(n) Since we constructing newHead to return.


If you like this article, please subscribe my blog by entering your email id. So that, you won’t miss any new posts from me. I made a progress to solve algorithm problems daily with detailed and diagrammatic explanations, so others can easily understand the solution. Still now, I solved daily and made blogs with detailed explanations. If you like to support my work, please subscribe my blog via email. Kindly ignore, if you are already subscribed😊

[jetpack_subscription_form show_only_email_and_button=”true” custom_background_button_color=”#0693e3″ custom_text_button_color=”undefined” submit_button_text=”Subscribe” submit_button_classes=”wp-block-button__link has-text-color has-background has-vivid-cyan-blue-background-button-color” show_subscribers_total=”false” ]

0 thoughts on “7) Reverse a Linked List – Iterative and Recursive approach”

  1. Pingback: 11) Palindrome Linked List – Developers Inspired

Leave a Comment

Your email address will not be published. Required fields are marked *