fork download
  1. #include <iostream>
  2. using namespace std;
  3. struct node{
  4. int data;
  5. struct node* next;
  6. };
  7. struct node* createnode(int val)
  8. {
  9. struct node* p=(struct node*)malloc(sizeof(struct node));
  10. p->data=val;
  11. p->next=NULL;
  12. return p;
  13.  
  14. }
  15. struct node* evenreverse(struct node* head)
  16. {
  17. struct node* head1=NULL;
  18. struct node* tail1=NULL;
  19. struct node* temp=head;
  20. if(head==NULL || head->next==NULL|| head->next->next==NULL)
  21. return head;
  22. while(temp && temp->next)
  23. {
  24. if(head1==NULL)
  25. {
  26. head1=tail1=temp->next;
  27. }
  28. else
  29. {
  30. tail1->next=temp->next;
  31. tail1=tail1->next;
  32. }
  33. temp->next=temp->next->next;
  34. temp=temp->next;
  35. }
  36. tail1->next=NULL;
  37. struct node* cur=head1;
  38. struct node* next=NULL;
  39. struct node* prev=NULL;
  40.  
  41. while(cur)
  42. {
  43. next=cur->next;
  44. cur->next=prev;
  45. prev=cur;
  46. cur=next;
  47. }
  48. struct node* second=prev;
  49. struct node* first=head;
  50. while(first)
  51. {
  52. temp=second->next;
  53. second->next=first->next;
  54. first->next=second;
  55. second=temp;
  56. first=first->next->next;
  57. }
  58. return head;
  59. }
  60. void printlinked(struct node* head)
  61. {
  62. while(head!=NULL)
  63. {
  64. cout<<head->data<<" ";
  65. head=head->next;
  66. }
  67. }
  68.  
  69. int main() {
  70. struct node* head=createnode(1);
  71. head->next=createnode(2);
  72. head->next->next=createnode(3);
  73. head->next->next->next=createnode(4);
  74. head->next->next->next->next=createnode(5);
  75. head->next->next->next->next->next=createnode(6);
  76. head=evenreverse(head);
  77. printlinked(head);
  78. return 0;
  79. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
1 6 3 4 5 2