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

Can comments be longer than one line?

706


What is the role of C++ shorthand's?

782


Why do we use constructor?

712


Is it possible to use a new for the reallocation of pointers ?

680


How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?how can you find the nodes with repetetive data in a linked list?

849






Will this c++ program execute or not?

702


What is recursion?

744


What is wrapper class in c++?

709


total amount of milk produced each morning and then calculates and outputs the number of cartons needed for this milk , the cost of producing the milk and the profit from producing this milk.

2163


Why is c++ is better than c?

617


Describe private, protected and public?

687


What is virtual function? Explain with an example

673


Is the declaration of a class its interface or its implementation?

799


Where must the declaration of a friend function appear?

640


What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required

669