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?..

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

{
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;
}
}

 Is This Answer Correct ? 14 Yes 1 No

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

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

{
node* cur = *head;

while (cur)
{
node* next = cur->next;
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?..

2 pointers are required....

 Is This Answer Correct ? 7 Yes 6 No

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

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

}

 Is This Answer Correct ? 1 Yes 2 No

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

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

}

 Is This Answer Correct ? 1 Yes 2 No

More C++ General Interview Questions

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

Explain the operation of overloading of an assignment operator.

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

What is difference between initialization and assignment?

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.

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

Describe new operator and delete operator?

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

What are references in c++?

Explain differences between alloc() and free()?

Is there a new/delete equivalent of realloc?

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

Categories
• STL (108)
• OOPS (592)
• C++ General (1546)