Easy to understand C++ Solution, 88ms, beats 95%
Runtime: 88 ms, faster than 95.78% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\nMemory Usage: 31.1 MB, less than 62.50% of C++ online
Runtime: 88 ms, faster than 95.78% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\nMemory Usage: 31.1 MB, less than 62.50% of C++ online submissions for Flatten a Multilevel Doubly Linked List.\n\n```\n/*\n// Definition for a Node.\nclass Node {\npublic:\n    int val;\n    Node* prev;\n    Node* next;\n    Node* child;\n\n    Node() {}\n\n    Node(int _val, Node* _prev, Node* _next, Node* _child) {\n        val = _val;\n        prev = _prev;\n        next = _next;\n        child = _child;\n    }\n};\n*/\nclass Solution {\npublic:\n    Node* flatten(Node* head) {\n        \n        flattenHelper(head);\n        return head;\n    }\n    \n    Node* flattenHelper(Node* head)\n    {\n        if(head == nullptr)\n            return head;\n        \n        while(head->next)\n        {\n            if(head->child)\n            {\n                Node* nextNode = head->next;\n                Node* end = flattenHelper(head->child);\n                head->next = head->child;\n                head->child->prev = head;\n                end->next = nextNode;\n                nextNode->prev = end;\n                head->child = nullptr;\n                head = nextNode;\n            }\n            else\n                head = head->next;\n        }\n        \n        if(head->child)\n        {\n            Node* end = flattenHelper(head->child);\n            head->next = head->child;\n            head->child->prev = head;\n            head->child = nullptr;\n        }\n        \n        return head;\n    }\n};
['Recursion']