knakul853

Untitled

Jul 17th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. /*
  2. knakul853
  3. */
  4.  
  5. class Solution {
  6. public:
  7.     Node* flatten(Node* head) {
  8.        
  9.        Node* cur = head;
  10.        
  11.         while(cur){
  12.            
  13.             if(!cur->child){
  14.                 cur = cur->next;
  15.                 continue;
  16.             }
  17.             else{
  18.                
  19.                 Node* pre = cur->next;
  20.                 Node* temp = cur->child;
  21.                 while(temp->next)
  22.                 {
  23.                     temp = temp->next;
  24.                 }
  25.                
  26.                 temp->next = pre;
  27.                 if(pre) pre->prev = temp;
  28.                 cur->next = cur->child;
  29.                
  30.                 cur->child->prev = cur;
  31.                 cur->child = NULL;
  32.                
  33.                 cur = cur->next;
  34.             }
  35.         }
  36.        
  37.         return head;
  38.     }
  39. };
Add Comment
Please, Sign In to add comment