fork download
  1. #include<stdio.h>
  2. struct node
  3. {
  4. int info;
  5. struct node* next;
  6. };
  7.  
  8. node* newNode(int data)
  9. {
  10. struct node* temp = new node;
  11. temp->info = data;
  12. temp->next=NULL;
  13. return (temp);
  14. }
  15.  
  16. node* reverse(node* start,int k)
  17. {
  18.  
  19. node *curr=start,*prev,*next,*end;
  20. int i=0;
  21. prev=end;
  22. end=start;
  23. if(curr==NULL)
  24. return NULL;
  25.  
  26. while(i<k && curr!=NULL)
  27.  
  28. {
  29. next=curr->next;
  30. curr->next=prev;
  31. prev=curr;
  32. curr=next;
  33. i++;
  34. }
  35. end->next=reverse(curr,k);
  36. return prev;
  37. }
  38.  
  39. void print(node* head)
  40. {
  41. node* ptr=head;
  42. while(ptr!=NULL)
  43. {
  44. printf("%d->",ptr->info);
  45. ptr=ptr->next;
  46. }
  47. printf("NULL");
  48. }
  49.  
  50. int main()
  51. {
  52. int k=2;
  53.  
  54. node* head=newNode(0);
  55. head->next=newNode(1);
  56. head->next->next=newNode(2);
  57. head->next->next->next=newNode(3);
  58. head->next->next->next->next=newNode(4);
  59. head->next->next->next->next->next=newNode(5);
  60. head->next->next->next->next->next->next=newNode(6);
  61. head->next->next->next->next->next->next->next=newNode(7);
  62. head->next->next->next->next->next->next->next->next=newNode(8);
  63. head->next->next->next->next->next->next->next->next->next=newNode(9);
  64. head->next->next->next->next->next->next->next->next->next->next=newNode(10);
  65.  
  66. head=reverse(head,k);
  67. print(head);
  68. return 0;
  69. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1->0->3->2->5->4->7->6->9->8->10->NULL