javascript stack
\nvar flatten = function(head) {\n    if (!head) return head;\n    let stack = []; //store all rest part of linkedlist nodes when has child\n    let cur = head;
```\nvar flatten = function(head) {\n    if (!head) return head;\n    let stack = []; //store all rest part of linkedlist nodes when has child\n    let cur = head;\n    while (cur){\n        if (cur.child){\n            if (cur.next) stack.push(cur.next);  //must check cur.next is null or not before added to stack\n            cur.next = cur.child;\n            cur.next.prev = cur; //because it is doubly linkedlist\n            cur.child = null; //already assigned to next so now no child anymore. set null\n        }\n        else if (!cur.next && stack.length!= 0){ //now reach tail of linkedlist \n            cur.next = stack.pop();\n            cur.next.prev = cur; // because it is doubly linkedlist\n        }\n        cur = cur.next;\n    }\n    return head; //return reference of head\n};\n```
['JavaScript']