🎉 One-stop destination for all your technical interview Preparation 🎉
Given a linked list of N nodes. The task is to reverse this list.
struct Node *reverseList(struct Node *head)
{
Node *curr = head;
Node *prev = NULL, *next = NULL;
while (curr != NULL)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
return prev;
}
struct Node *reverse(struct Node *head)
{
if (head == NULL || head->next == NULL)
return head;
Node *rest = reverse(head->next);
head->next->next = head;
head->next = NULL;
return rest;
}