How to reverse all the no using a single link list?

Questions by GeekAdmin   answers by GeekAdmin

Showing Answers 1 - 4 of 4 Answers

sriv

  • Oct 13th, 2006
 

Implement linked list as a stack. Last in first out. Enlist and delist at the front.

  Was this answer useful?  Yes

Rohit

  • Oct 15th, 2006
 

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;

  }

  Was this answer useful?  Yes

f2003062

  • Oct 30th, 2006
 

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

  Was this answer useful?  Yes

csabill

  • Nov 2nd, 2006
 

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

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions