Java Solution using a Stack
We can just use a stack to pile the next nodes when there is a child node. For it to be valid remember to set the child node to null and change the prev pointer
We can just use a stack to pile the next nodes when there is a child node. For it to be valid remember to set the child node to null and change the prev pointers (while validating that they are not null)\n\n```\n    public Node flatten(Node head) {\n        Stack<Node> prevs = new Stack();\n        Node cur = head;\n        while(cur != null) {\n            if(cur.child != null) {\n                prevs.add(cur.next);\n                cur.next = cur.child;\n                cur.child.prev = cur;\n                cur.child = null;\n            }\n            if (cur.next == null && !prevs.isEmpty()){\n                cur.next = prevs.pop();\n                if (cur.next != null) cur.next.prev = cur;\n            }\n            cur = cur.next;\n        }\n        return head;\n    }\n```
[]