fork download
  1. #include<iostream>
  2. using namespace std;
  3. struct node
  4. {
  5. int val;
  6. node *next;
  7. };
  8. void traverse(node *head)
  9. {
  10. node *temp=head;
  11. while(temp)
  12. {
  13. cout<<temp->val<<" ";
  14. temp=temp->next;
  15. }
  16. }
  17. int main()
  18. {
  19. node *head=NULL;
  20. for(int i=0; i<5; i++)
  21. {
  22. node *ptr=new node;
  23. cin>>ptr->val;
  24. ptr->next=NULL;
  25. ptr->next=head;
  26. head=ptr;
  27. }
  28. traverse(head);
  29. cout<<endl;
  30. node *prev=NULL;
  31. node *cur=head;
  32. node *n=head->next;
  33. while(cur)
  34. {
  35. n=cur->next;
  36. cur->next=prev;
  37. prev=cur;
  38. cur=n;
  39. }
  40. head=prev;
  41. traverse(head);
  42. }
Success #stdin #stdout 0.01s 5308KB
stdin
Standard input is empty
stdout
0 0 0 0 0 
0 0 0 0 0