How many pointers are required to reverse a link list?

Answer Posted / prits

Using 3 pointers:
curr, next, result pointers, curr points to current node,
next obviously points to the next node, result points to
the new reversed linked list

void reverse_single_linked_list(struct node** headRef)
{
struct node* result = NULL;
struct node* current = *headRef;
struct node* next;
while (current != NULL)
{
next = current->next; // tricky: note the next node
current->next = result; // move the node onto the result
result = current;
current = next;
}
*headRef = result;
}

Is This Answer Correct ?    14 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Which compiler does turbo c++ use?

599


What is singleton pattern in c++?

544


int age=35; if(age>80) {Console.WriteLine("Boy you are old");} else {Console.WrieLine("That is a good age");}

828


Why ctype h is used in c++?

520


What do you mean by “this” pointer?

613






What is the difference between new() and malloc()?

614


Will a C compiler always compile C++ code a) Yes b) No c) Only optimized compilers

608


What is a constant reference?

613


What are the extraction and insertion operators in c++? Explain with examples.

641


What are the various operations performed on stack?

627


What is pointer -to-members in C++? Give their syntax?

594


Can a new be used in place of old mallocq? If yes, why?

629


Why do we need pointers?

577


What are the advantage of using register variables?

635


What is c++ prototype?

580