DEV Community

Saranya R
Saranya R

Posted on

Reverse a Linked List

there are three methods to reverse a linked list using stack, Iterative and Recursion method

While stack and recursion method is easier understand iterative method is easier to implement

Approach Explanation o( ̄▽ ̄)o

  1. Start with two pointers: 1.1. curr → the current node you’re processing. 1.2. prev → the node that will become the new "next" for curr. 1.3. Initially None.
  2. For each node: 2.1. Save the next node (nextNode = curr.next). 2.2. Reverse the link (curr.next = prev). 2.3. Move prev forward (prev = curr). 2.4. Move curr forward (curr = nextNode).
  3. When curr becomes None, prev points to the new head of the reversed list.

Method Used: (〜 ̄▽ ̄)〜

  • Pointer manipulation
  • Iterative traversal
  • In-place reversal
  • Efficient runtime

Top comments (0)