Write a program to reverse a linked list?

Answers were Sorted based on User's Feedback



Write a program to reverse a linked list?..

Answer / surendra

suppose p is the head pointer.


r=NULL;
while(p)
{
q=r;
r=p;
p=p->next;
r->next=q;
}
p=r;

Is This Answer Correct ?    16 Yes 7 No

Write a program to reverse a linked list?..

Answer / jithin

#include<iostream>
using namespace std;
class Link;
class Node
{
int value;
Node * next;
friend class Link;
};
class Link
{
Node * start;
public:
Link();
void add();
void display();
void reverse();
};

Link::Link()
{
start=NULL;
}

void Link::add()
{
int value;
Node * node=new Node;
cout<<"Enter the number:";
cin>>node->value;
node->next=NULL;
if(start==NULL)
{
start=node;
}
else
{
Node * temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}


}

void Link::display()
{
Node * temp=start;
while(temp->next!=NULL)
{
cout<<temp->value<<"-->";
temp=temp->next;
}
cout<<temp->value<<"\n";
}

void Link::reverse()
{
Node * temp,* temp1,* temp2;
temp=start;
temp2=NULL;
while(temp)
{
temp1=temp->next;
if(temp1==NULL)
start=temp;
temp->next=temp2;
temp2=temp;
temp=temp1;
}

}
main()
{
int i=0;

Link link;
while(i<6)
{
link.add();
i++;
}
link.display();
link.reverse();
cout<<"======================After reversing\n";
link.display();
}

Is This Answer Correct ?    8 Yes 0 No

Write a program to reverse a linked list?..

Answer / prakash d

struct node *ptr1,*ptr2,*ptr3;

ptr1=start; //pointer points to starting node.
ptr2=ptr1->next;
ptr3=ptr2->next;

ptr1->next=NULL;

ptr2->next=ptr1;

while(ptr3!=NULL)
{
ptr1=ptr2;
ptr2=ptr3;
ptr3=ptr3->next;
ptr2->next=ptr1;
}
start=ptr2;

Is This Answer Correct ?    13 Yes 6 No

Write a program to reverse a linked list?..

Answer / murali

/* Error Checking is not done */

#include <stdio.h>
typedef struct node {
char ch;
struct node *next;
} list;
list* addNode(const list *start, char ch) {
list *ll;
ll = (list *)start;
while( ll->next != NULL ) { ll = ll->next; }
ll->next = (list *) malloc(sizeof(list));
ll->next->ch = ch;
ll->next->next = NULL;
return ll->next;
}
void printList(const list *start) {
list *ll;
ll = (list *)start;
while ( ll->next != NULL ) {
printf(" %c --> ", ll->ch );
ll = ll->next;
}
printf(" %c --> ", ll->ch );
printf( " NULL ");
}
void reverse(list *a, list *b) {
if( b->next != NULL )
reverse(b, b->next);
b->next = a;
a->next = NULL;
}
int main() {

list *end;
list *start = (list *) malloc(sizeof(list));
start->ch = 'A';
start->next = NULL;

addNode(start, 'B');
addNode(start, 'C');
addNode(start, 'D');
addNode(start, 'E');
end = addNode(start, 'F');

printList(start);
printf("\n");

reverse(start, start->next);

printList(end);
printf("\n");

return 0;
}

Is This Answer Correct ?    9 Yes 5 No

Write a program to reverse a linked list?..

Answer / bragaadeesh

Program to reverse a singly list ITERATIVELY,

http://www.technicalypto.com/2010/01/java-program-to-reverse-singly-linked.html

Program to reverse a linked list RECURSIVELY

http://www.technicalypto.com/2010/03/reverse-singly-linked-list-recursively.html

Is This Answer Correct ?    3 Yes 0 No

Write a program to reverse a linked list?..

Answer / bipin pandey

node *reverse(node *first)


{
node *cur,*temp;


cur=NULL;

while(first!=NULL)


{temp=first;

first=first->link;

temp->link=cur;

cur=temp;
}

return cur;

}

Is This Answer Correct ?    5 Yes 4 No

Write a program to reverse a linked list?..

Answer / jithin

reverse(Node * previous, Node * Current)
{
start = current;
if(current !=null)
{
reverse(current, current->next);
current->next = previous;
}

}

Is This Answer Correct ?    2 Yes 2 No

Write a program to reverse a linked list?..

Answer / ajaypal singh badgujar

#include<iostream>
using namespace std;
class Link;
class Node
{
int value;
Node * next;
friend class Link;
};
class Link
{
Node * start;
public:
Link();
void add();
void display();
void reverse();
};

Link::Link()
{
start=NULL;
}

void Link::add()
{
int value;
Node * node=new Node;
cout<<"Enter the number:";
cin>>node->value;
node->next=NULL;
if(start==NULL)
{
start=node;
}
else
{
Node * temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=node;
}


}

void Link::display()
{
Node * temp=start;
while(temp->next!=NULL)
{
cout<<temp->value<<"-->";
temp=temp->next;
}
cout<<temp->value<<"\n";
}

void Link::reverse()
{
Node * temp,* temp1,* temp2;
temp=start;
temp2=NULL;
while(temp)
{
temp1=temp->next;
if(temp1==NULL)
start=temp;
temp->next=temp2;
temp2=temp;
temp=temp1;
}

}
main()
{
int i=0;

Link link;
while(i<6)
{
link.add();
i++;
}
link.display();
link.reverse();
cout<<"======================After reversing\n";
link.display();
}

Is This Answer Correct ?    0 Yes 2 No

Post New Answer

More C++ General Interview Questions

What is the standard template library (stl)?

3 Answers  


What parameter does the constructor to an ofstream object take?

0 Answers  


Explain how an exception handler is defined and invoked in a Program.

0 Answers  


What kind of jobs can I get with c++?

0 Answers  


pls help.. paper bills.. 1000, 500, 100, 50, 20, 10, 5, 1.. create a program that will count all the paper bills in the number being input.. example: enter a number: 3886 there is/are: 3 ->1000 1 ->500 3 ->100 1 ->50 1 ->20 1 ->10 1 ->5 1 ->1 example2: enter a number: 728 there is/are: 0 ->1000 1 ->500 2 ->100 0 ->50 1 ->20 0 ->10 1 ->5 3 ->1

4 Answers  


When is the last time you coded in C/C++? What is the most lines of original C/C++ code you have personally written in one project? How confident are you in your ability to write C or C++ without a reference?

1 Answers   Microsoft,


Can you explicitly call a destructor on a local variable?

0 Answers  


What are the main differences between C and C++?

1 Answers  


Write a program that read 2o numbers in and array and output the second largest number. Can anybody help??

7 Answers  


Which software is best for programming?

0 Answers  


Why did you leave your last job?

2 Answers  


What is scope in c++ with example?

0 Answers  


Categories