How many pointers are required to reverse a link list?




Answers were Sorted based on User's Feedback



How many pointers are required to reverse a link list?..

Answer / 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

How many pointers are required to reverse a link list?..

Answer / patrick

You cannot must have a pointer to a pointer if you want to
modify the pointer.

void reverse(node** head)
{
node* cur = *head;
*head = null;

while (cur)
{
node* next = cur->next;
cur->next = *head;
*head = cur;
cur = next;
}
}

Besides the head pointer, you will need two local pointers.

Is This Answer Correct ?    4 Yes 1 No

How many pointers are required to reverse a link list?..

Answer / selva

2 pointers are required....

Is This Answer Correct ?    7 Yes 6 No




How many pointers are required to reverse a link list?..

Answer / vivek

using 2 pointer:
void reverse(node* head_in_out)
{
if(head_in_out)
{
node* aCurr = head_in_out;
node* aNext = NULL;
while (aCurr)
{
head_in_out = aCurr->next;
aCurr->next = aNext;
aNext = aCurr;
aCurr = head_in_out;
}
}

}

Is This Answer Correct ?    1 Yes 2 No

How many pointers are required to reverse a link list?..

Answer / vivek

# 3 using 2 pointer:
void reverse(node* head_in_out)
{
if(head_in_out)
{
node* aCurr = head_in_out;
node* aNext = NULL;
while (aCurr)
{
head_in_out = aCurr->next;
aCurr->next = aNext;
aNext = aCurr;
aCurr = head_in_out;
}
head_in_out = aNext; // Bug in above 3rd answer.
}

}

Is This Answer Correct ?    1 Yes 2 No

Post New Answer



More C++ General Interview Questions

Should the member functions which are made public in the base class be hidden?

0 Answers  


Explain the operation of overloading of an assignment operator.

0 Answers  


Write a program to calculate the BMI of a person using the formula BMI = weight/height2.

2 Answers  


What is difference between initialization and assignment?

12 Answers   Infosys, HCL, HP,


Assume studentNames and studentIDs are two parallel arrays of size N that hold student data. Write a pseudocode algorithm that sorts studentIDs array in ascending ID number order such that the two arrays remain parallel.

0 Answers  






What will happen if I allocate memory using "new" and free it using "free" or allocate sing "calloc" and free it using "delete"?

3 Answers  


Describe new operator and delete operator?

0 Answers  


Write a program to add three numbers in C++ utilizing classes.

0 Answers   TCS,


What are references in c++?

0 Answers  


Explain differences between alloc() and free()?

0 Answers  


Is there a new/delete equivalent of realloc?

1 Answers  


Explain the ISA and HASA class relationships. How would you implement each in a class design?

3 Answers  






Categories