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
Can comments be longer than one line?
What is the role of C++ shorthand's?
Why do we use constructor?
Is it possible to use a new for the reallocation of pointers ?
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?
Will this c++ program execute or not?
What is recursion?
What is wrapper class in c++?
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.
Why is c++ is better than c?
Describe private, protected and public?
What is virtual function? Explain with an example
Is the declaration of a class its interface or its implementation?
Where must the declaration of a friend function appear?
What doescout<<(0==0) print out a) 0 b) 1 c) Compiler error: Lvalue required