fork download
  1. # include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct Node {
  6. int data;
  7. struct Node *next;
  8.  
  9. }*head=NULL;
  10.  
  11. void display(struct Node *p)
  12. {
  13. while(p!=NULL)
  14. {
  15. cout<<p->data<<" ";
  16. p=p->next;
  17. }
  18. }
  19.  
  20.  
  21. void Reverse(struct Node *p, struct Node *q)
  22. {
  23. // cout<<p -> data<<":"<<q -> data;
  24. if(q -> next)
  25. {
  26. Reverse(q,q->next);
  27. }
  28. else
  29. head=q;
  30. q->next=p;
  31. p->next = NULL;
  32. }
  33.  
  34. int main()
  35. {
  36. struct Node *t,*t1,*t2;
  37. t=new struct Node;
  38. t1=new struct Node;
  39. t2=new struct Node;
  40.  
  41. t->data=1;
  42. t->next=NULL;
  43.  
  44. head=t;
  45.  
  46. t1->data=2;
  47. t1->next=NULL;
  48.  
  49. head->next=t1;
  50.  
  51. t2->data=3;
  52. t2->next=NULL;
  53.  
  54. head->next->next=t2;
  55.  
  56. display(head);
  57.  
  58. //struct Node *q=NULL;
  59.  
  60. Reverse(head,head -> next);
  61.  
  62. display(head);
  63.  
  64. return 0;
  65. }
Success #stdin #stdout 0s 4956KB
stdin
Standard input is empty
stdout
1 2 3 3 2 1