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

the first character in the variable name must be an a) special symbol b) number c) alphabet

0 Answers  


What are dynamic type checking?

0 Answers  


What does the following code do: int c=0; cout< a) Undefined *Updated* b) 01 c) 00

0 Answers  


Difference between shift left and shift right?

1 Answers   Symphony,


what is a reference variable in C++?

0 Answers  






To which numbering system can the binary number 1101100100111100 be easily converted to?

0 Answers  


Is empty stack c++?

0 Answers  


what is Loop function? What are different types of Loops?

0 Answers  


When do we run a shell in the unix system?

0 Answers  


What is a template in c++?

0 Answers  


What do you mean by friend class & friend function in c++?

0 Answers  


Is it possible to have a recursive inline function in c++?

0 Answers  


Categories