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

Is linux written in c or c++?

0 Answers  


What are associate containers?

0 Answers  


What is the difference between an external iterator and an internal iterator? Describe an advantage of the external iterator.

0 Answers  


How to construct muliton object

2 Answers   Symphony, TCS,


What is the use of map in c++?

0 Answers  






What is the difference between mutex and binary semaphore?

0 Answers  


Why is standard template library used?

0 Answers  


How do you add an element to a set in c++?

0 Answers  


Difference between struct and class in terms of access modifier.

0 Answers  


Explain what are single and multiple inheritances in c++?

0 Answers  


class X { public: int x; static void f(int z); }; void X::f(int y) {x=y;} What is the error in the sample code above? a) The class X does not have any protected members. b) The static member function f() accesses the non-static z. c) The static member function f() accesses the non-static x. d) The member function f() must return a value. e) The class X does not have any private members.

2 Answers   Quark,


What is abstraction with real time example?

0 Answers  


Categories