Explain the most efficient method to reverse a linked list?
Submitted by: MuhammadTo 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;
}
Submitted by: Muhammad
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;
}
Submitted by: Muhammad
Read Online Data Structure Linked list Job Interview Questions And Answers
Top Data Structure Linked list Questions
☺ | Suppose In an integer array, there is 1 to 100 number, out of one is duplicate, how to find? |
☺ | What is circular linked list? |
☺ | Explain linked list using C++ with an example? |
☺ | Explain the steps to insert data into a singly linked list? |
☺ | Explain how to find 3rd element from end in a linked list in one pass? |
Top Data Structure Categories
☺ | Data Center Manager Interview Questions. |
☺ | Creative UI/UX Designers Interview Questions. |
☺ | Sort And Searching Interview Questions. |
☺ | Data Structure Linked list Interview Questions. |
☺ | Data Structure Arrays Interview Questions. |