Skip to main content

Reverse Linked List

Problem

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

Solution Approach

Click - to see solution code
class Solution {
ListNode* root;

public:
void reverse(ListNode* temp) {
if (temp->next->next == NULL) {
temp->next->next = temp;
root = temp->next;
return;
} else
reverse(temp->next);
temp->next->next = temp;
}

ListNode* reverseList(ListNode* head) {
if (head == NULL) return NULL;
if (head->next == NULL) return head;
ListNode* temp = head;
reverse(temp);
head->next = NULL;
return root;
}
};