fork download
  1. Node<int>* rearrange(Node<int> *head){
  2. // Write your code here
  3. if(head == NULL){
  4. return NULL;
  5. }
  6. else if(head->next == NULL){
  7. return head;
  8. }
  9. Node<int> *fast = head;
  10. Node<int> *slow = head;
  11. while(fast == NULL && fast->next == NULL){
  12. fast = fast->next->next;
  13. slow = slow->next;
  14. }
  15. Node<int> *head2 = slow->next;
  16. slow->next = NULL;
  17. Node<int> *head1 = head;
  18. //reverse 2nd ll
  19. Node<int> *curr = head2;
  20. Node<int> *n;
  21. Node<int> *prev = NULL;
  22. while(curr!=NULL){
  23. n = curr->next;
  24. curr->next = prev;
  25. prev = curr;
  26. curr = n;
  27. }
  28. head2 = prev;
  29. //merge
  30. Node<int> *temp_head = NULL;
  31. Node<int> *temp = temp_head;
  32. while(head1 || head2){
  33. if(head1){
  34. temp->next = head1;
  35. temp = temp->next;
  36. head1 = head1->next;
  37. }
  38. if(head2){
  39. temp->next = head2;
  40. temp = temp->next;
  41. head2 = head2->next;
  42. }
  43. }
  44. temp_head->next = temp;
  45. return temp;
  46. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:1: error: ‘Node’ does not name a type
 Node<int>* rearrange(Node<int> *head){
 ^~~~
stdout
Standard output is empty