LikedList Questions: Reverse a Linked List – Iterative version

In this series of posts, I will discuss coding questions on the LinkedList Data structure.
The posts in this series will be organized in the following way,

Question Link ❓
Possible Explanation ?
Documented C++ Code ?
Time and Space Complexity Analysi…

In this series of posts, I will discuss coding questions on the LinkedList Data structure.
The posts in this series will be organized in the following way,

  1. Question Link ❓
  2. Possible Explanation ?
  3. Documented C++ Code ?
  4. Time and Space Complexity Analysis ⌛?



The Question

Given the head of a singly linked list, reverse the list, and return the reversed list.

https://leetcode.com/problems/reverse-linked-list/

? Give yourself at least 15-20 mins to figure out the solution 🙂

??‍♂️ Tip: You need to be good at pointer manipulations.



Explanation

We will go through every node and make it point to its preceding node. The idea is fairly simple, it’s all about pointer manipulations and how you will do it. Here’s the algorithm,

  • Initialize three pointers, prev = null, nex = null, cur = head. At every iteration cur will point to the current node, prev will point to its preceding node and nex will point to its succeeding node.
  • while cur ≠ null i.e until we haven’t reversed every node

    • nex = cur ( Before we change cur's next, take hold of its succeeding node )
    • cur → next = prev ( Reverse the link )
    • prev = cur (Update prev for following iteration )
    • cur = nex (Update cur for following iteration )
  • Make head = prev ( prev points to the first node of reversed linked list )

Here’s a dry run on a list of 5 elements to make things more clear,

Dry run of algorithm



C++ Code



Definition of LinkedList

//Definition for singly-linked list.
struct ListNode
{
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};



Solution

ListNode *reverseList(ListNode *head)
    {

        ListNode *prev = nullptr;
        ListNode *cur = head;
        ListNode *next = nullptr;

        //while cur is pointing to a node
        while (cur)
        {
            //get access to the node ahead
            next = cur->next;

            //break the forward link
            cur->next = prev;

            //moving prev ahead
            prev = cur;

            //updating current node
            cur = next;
        }
        //After the loop, prev will be pointed to the required node
        head = prev;
        return head;
    }



Complexity Analysis



Time Complexity: O(n)

We are traveling every node.



Space Complexity: O(1)

We didn’t use any extra space.

? Can you think of a recursive solution? I will answer this in the next post.


Print Share Comment Cite Upload Translate
APA
Kathan Vakharia | Sciencx (2024-03-28T15:38:04+00:00) » LikedList Questions: Reverse a Linked List – Iterative version. Retrieved from https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/.
MLA
" » LikedList Questions: Reverse a Linked List – Iterative version." Kathan Vakharia | Sciencx - Sunday July 4, 2021, https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/
HARVARD
Kathan Vakharia | Sciencx Sunday July 4, 2021 » LikedList Questions: Reverse a Linked List – Iterative version., viewed 2024-03-28T15:38:04+00:00,<https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/>
VANCOUVER
Kathan Vakharia | Sciencx - » LikedList Questions: Reverse a Linked List – Iterative version. [Internet]. [Accessed 2024-03-28T15:38:04+00:00]. Available from: https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/
CHICAGO
" » LikedList Questions: Reverse a Linked List – Iterative version." Kathan Vakharia | Sciencx - Accessed 2024-03-28T15:38:04+00:00. https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/
IEEE
" » LikedList Questions: Reverse a Linked List – Iterative version." Kathan Vakharia | Sciencx [Online]. Available: https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/. [Accessed: 2024-03-28T15:38:04+00:00]
rf:citation
» LikedList Questions: Reverse a Linked List – Iterative version | Kathan Vakharia | Sciencx | https://www.scien.cx/2021/07/04/likedlist-questions-reverse-a-linked-list-iterative-version/ | 2024-03-28T15:38:04+00:00
https://github.com/addpipe/simple-recorderjs-demo