Java recursive solution : Similar to flatten a binary tree
Notice that if we perform a preorder traversal of the linked list, we would have the nodes in the desired order. In other words, if we perform a \'reverse posto
Notice that if we perform a preorder traversal of the linked list, we would have the nodes in the desired order. In other words, if we perform a \'reverse postorder\', we would get the nodes in reverse order. We would keep track of those nodes using \'prev\' and append them to the next node we get from the call stack. Similarly, we would also set the prev of \'prev\' to that node and child of that node to null.\n\n```\nclass Solution {\n    Node prev = null;\n    \n    public Node flatten(Node head) {\n        if (head == null) return head;\n        \n        flatten(head.next);\n        flatten(head.child);\n        \n        if (prev != null){\n            head.next = prev;\n            prev.prev = head;\n            head.child = null;\n        }\n        \n        prev = head;\n        \n        return head;\n    }\n}\n```\n\n
['Recursion']