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