✅ Java | 2 Solutions - Iterative, Recursive
Approach 1: Recursive\n\n\n// Time complexity: O(n)\n// Space complexity: O(n)\n\nclass Solution {\n    public Node flatten(Node head) {\n\t\thelper(head, null)
**Approach 1: Recursive**\n\n```\n// Time complexity: O(n)\n// Space complexity: O(n)\n\nclass Solution {\n    public Node flatten(Node head) {\n\t\thelper(head, null);\n        return head;\n    }\n\n    public Node helper(Node curr, Node prev) {\n        if (curr == null)\n            return prev;\n        \n        if (curr.child == null)\n            return helper(curr.next, curr);\n        \n        Node next = curr.next;\n        curr.next = curr.child;\n\t\t\n        curr.child.prev = curr;\n        curr.child = null;\n\t\t\n        Node tail = helper(curr.next, curr);\n        tail.next = next;\n\t\t\n        if (next != null)\n            next.prev = tail;\n        \n        return helper(next, tail);\n    }\n}\n```\n\n**Approach 2: Iterative**\n\n```\n// Time complexity: O(n)\n// Space complexity: O(1)\n\npublic Node flatten(Node head) {\n\tNode curr = head;\n\n\twhile (curr != null) {\n\t\tif (curr.child != null) {\n\t\t\tNode tail = curr.child;\n\n\t\t\twhile (tail.next != null)\n\t\t\t\ttail = tail.next;\n\n\t\t\ttail.next = curr.next;\n\t\t\tif (curr.next != null)\n\t\t\t\tcurr.next.prev = tail;\n\n\t\t\tcurr.next = curr.child;\n\t\t\tcurr.child.prev = curr;\n\t\t\t\n\t\t\tcurr.child = null;\n\t\t}\n\n\t\tcurr = curr.next;\n\t}\n\n\treturn head;\n}\n```\n\n**Please upvote if you find this post useful. Happy Coding!**
[]