GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Concepts  >  Data Structures
Go To First  |  Previous Question  |  Next Question 
 Data Structures  |  Question 153 of 202    Print  
How to reverse all the no using a single link list?

  
Total Answers and Comments: 4 Last Update: November 02, 2006     Asked by: GeekAdmin 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
October 13, 2006 02:10:10   #1  
sriv        

RE: how to reverse all the no using a single link list...
Implement linked list as a stack. Last in first out. Enlist and delist at the front.
 
Is this answer useful? Yes | No
October 15, 2006 00:52:01   #2  
Rohit        

RE: how to reverse all the no using a single link list...

A simple strategy .....

Cut the first node and make it's next pointer NULL.

For rest of the nodes cut each node and attach it to the previous node.

Code:

void reverse(int *st)

{

int *pt1 *pt2;

pt1 st; st st->next; pt1->next NULL;

while (st ! NULL)

{

pt2 st; st st->next; pt2->next pt1; pt1 pt2;

}

st pt1;

}


 
Is this answer useful? Yes | No
October 30, 2006 05:42:44   #3  
f2003062 Member Since: October 2006   Contribution: 7    

RE: how to reverse all the no using a single link list...
A quite different way..! but same as the previous logic written by Rohit..!struct linkedlist { int x; struct linkedlist* next;}*head;Assume that head contains some nos ...example: 1->2->3->4->NULLThe nos are stored as the following..head->x 1head->next->x 2head->next->next->x 3head->next->next->next->x 4head->next->next->next->next NULLTo reverse the nos...linkedlist* reverse(linkedlist* head){ linkedlist *tail *temp; tail temp head; temp->next null; while(tail->next ! NULL) { tail tail->next; head tail; head->next temp; temp head; } return head; // the reversed nos are now in head}After the execution of the above function...The result will be like this...head->x 4head->next->x 3head->next->next->x 2head->next->next->next->x 1head->next->next->next->next NULL
 
Is this answer useful? Yes | No
November 02, 2006 08:02:26   #4  
csabill        

RE: how to reverse all the no using a single link list...
void reverse() { Node < T > *currentNode head *nextNode currentNode->next; while(nextNode) { Node < T > *newNextNode nextNode->next; nextNode->next currentNode; currentNode nextNode; nextNode newNextNode; } tail head; tail->next 0; head currentNode; }
 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape