Data Structure Linked list Question: Download Linked list PDF

Explain the most efficient method to reverse a linked list?

Tweet Share WhatsApp

Answer:

To call the function:
newHead = reverse(head);

struct Node *reverse(Node *curp)
{
static struct Node *head = curp;
static struct Node *revHead = NULL;
if (curp == NULL)
return NULL;
if (curp->next == NULL)
revHead = curp;
else
reverse(curp->next)->next = curp;
if (curp == head) {
curp->next = NULL;
return revHead;
}
else
return curp;
}

Download Linked list PDF Read All 22 Linked list Questions
Previous QuestionNext Question
Do you know how to reverse String in Java?Explain the steps to insert data into a singly linked list?