Simple Java Iteration solution beats 100% with explanation
We can use a stack to store the next node when the current node has a child, so we can go back to it when we reach to the end of the current list. \n\n\n    pub
We can use a stack to store the next node when the current node has a child, so we can go back to it when we reach to the end of the current list. \n\n\n    public Node flatten(Node head) {\n        //We can use a stack to store the next node when the current node has a child, so we can go back to it when we reach to the end of the current list.\n        Stack<Node> stack = new Stack<>();\n        \n\t\t// Keep the head pointer and traverse the list using current node (curNode)\n        Node curNode = head;\n        \n\t\t\n        while(curNode != null){\n                \n            if(curNode.child != null){\n\t\t\t    // if the current node has a child, then add the next node to the stack\n                stack.add(curNode.next);\n\t\t\t\t// point the next node to the child\n                curNode.next = curNode.child;\n\t\t\t\t// remove the child\'s pointer\n                curNode.child = null;\n            }\n\t\t\t\n            // Determine which is the next node, if reached to the end of the current list and the stack is NOT empty, then pop the lastest node to be the next. \n            Node next = (curNode.next == null && !stack.isEmpty()) ? stack.pop() : curNode.next;\n            \n            if(next != null){\n\t\t\t    // make sure all the pointers are correct\n                next.prev = curNode;   \n                curNode.next = next;\n            }\n           \n            curNode = next;\n        }\n        return head;\n    }\n
['Stack', 'Doubly-Linked List', 'Iterator', 'Java']