This post is completed by 1 user

  • 0
Add to List
Medium

31. Reverse a Linked List Using Recursion

Objective: Reverse the given linked list using recursion

Example:

Input: -10-8-6-4-2
Output: -2-4-6-8-10
Recursion:
  • Recursively traverse the linked list until you reach the last node.
  • Set the last node as the new head of the list.
  • As the recursion unwinds, the remaining nodes will be returned in reverse order.
  • Reconnect each node to the previous one as they are returned, effectively reversing the pointers.
  • Continue this process until all nodes have been reconnected in reverse order.
 

Output:

Input: -10-8-6-4-2
Output: -2-4-6-8-10