## Similar Problems

Similar Problems not available

# Reverse Linked List - Leetcode Solution

LeetCode: Reverse Linked List Leetcode Solution

Difficulty: Easy

Topics: linked-list

The Reverse Linked List problem on LeetCode is a popular problem that involves reversing the order of a singly linked list. This problem is rated as "Easy" difficulty on the LeetCode website and is a great problem for beginners to practice with.

The problem statement is as follows:

"Reverse a singly linked list."

Example:

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

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

To solve this problem, we can use the iterative or the recursive approach. However, the iterative approach is more efficient and faster.

Iterative Solution

In the iterative solution, we need to traverse the linked list from the beginning and keep reversing the pointers until we reach the end of the list. We can maintain three pointers while traversing the list - current node, previous node, and next node.

Algorithm:

- Initialize previous node to NULL, current node to head, and next node to NULL.
- Traverse the linked list with the help of the current node.
- At each iteration, assign current->next = previous.
- Move to the next node with the help of the next pointer.
- Update previous node to the current node.
- Continue traversing the linked list until the current node is NULL.
- Return the new head, which is the previous node.

Code:

```
ListNode* reverseList(ListNode* head) {
ListNode *prev = NULL, *current = head, *next = NULL;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
return prev;
}
```

Time Complexity: O(n)

Space Complexity: O(1)

The above implementation takes O(n) time complexity, where n is the number of nodes in the linked list. The space complexity of the algorithm is O(1).

Recursive Solution

In the recursive solution, we reverse the list from the current node to the end of the list. Once we reach the end of the list, we update the pointer of the previous node to the current node.

Algorithm:

- If head is NULL or the list is empty, return head.
- Recursively reverse the rest of the list from the next node.
- Once we reach the end of the list, update the pointer for the previous node to the current node.
- Return the new head, which is the current node.

Code:

```
ListNode* reverseList(ListNode* head) {
if (head == NULL || head->next == NULL) {
return head;
}
ListNode* newHead = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return newHead;
}
```

Time Complexity: O(n)

Space Complexity: O(n)

The above implementation takes O(n) time complexity, where n is the number of nodes in the linked list. The space complexity of the algorithm is O(n) because we are using recursion. Each recursive call adds a new call frame to the call stack, which takes up space. This can lead to a stack overflow error for large input sizes.

Conclusion:

In conclusion, the Reverse Linked List problem is a great practice problem for beginners to get started with linked lists. The iterative solution is more efficient and faster since it has a space complexity of O(1). However, the recursive solution is also a good implementation to understand the concept of recursion.

## Reverse Linked List Solution Code

`1`