Java | 0ms | easy to understand
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/*\n// Definition for a Node.\nclass Node {\n    public int val;\n    public Node prev;\n    public Node next;\n    public Node child;\n};\n*/\n\nclass Solution {\n    public Node flatten(Node head) {\n        Node temp=head;\n        while(temp!=null)\n        {\n            if(temp.child==null)temp=temp.next;\n            else{\n                if(temp.next!=null)\n                link(temp.child,temp.next);\n                temp.next=temp.child;\n                temp.child.prev=temp;\n                temp.child=null;\n            }\n        }\n        return head;\n        \n    }\n    public void link(Node n1,Node n2)\n    {\n        while(n1.next!=null)n1=n1.next;\n        n1.next=n2;\n        n2.prev=n1;\n    }\n}\n```
['Java']