How to Reverse a Linked List?

Its all here

Given a pointer to the head node of a linked list, the task is to reverse the linked list. The links between the nodes need to be changed in order to reverse the list.

Problem Statement

Recursive Approach

Divide the linked list in two parts, the first node and the rest, and call the recursion for the second part while maintaining the connection between them.

Time complexity: O(N), where N is the size of linked list.  Space complexity: O(1)

Here, we'll use 3 variables i.e, prevNode, head, and nextNode.    - prevNode to NULL   - nextNode can stay empty.

Iterative Approach

Time complexity: O(N), where N is array size Space complexity: O(1)

How to implement these approaches in different programming languages?