nikunjsoni

430

Mar 17th, 2021
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. public:
  5.     int val;
  6.     Node* prev;
  7.     Node* next;
  8.     Node* child;
  9. };
  10. */
  11.  
  12. class Solution {
  13. public:
  14.     Node* flatten(Node* head) {
  15.         for(Node *curr=head; curr; curr=curr->next){
  16.             if(curr->child){
  17.                 Node *next = curr->next;
  18.                 Node *child = curr->child;
  19.                 child->prev = curr;
  20.                 curr->next = child;
  21.                 curr->child = NULL;
  22.                 while(child->next)
  23.                     child = child->next;
  24.                 child->next = next;
  25.                 if(next)
  26.                     next->prev = child;
  27.             }
  28.         }
  29.         return head;
  30.     };
  31. };
RAW Paste Data