Java 1ms Recursion beats 100% with explaination
Go through the linked list;\nIf \n1. Node cur.child == null, skip, don\'t go into recursion.\n2. Node cur.child != null, recurse and get child\'s tail node. Nod
Go through the linked list;\nIf \n1. Node cur.child == null, skip, don\'t go into recursion.\n2. Node cur.child != null, recurse and get child\'s tail node. Node tmp = cur.next, (1)connect cur <-> child, (2)connect child\'s tail <->tmp.\n\nThe helper function returns the tail node of current level.\n\n```\npublic Node flatten(Node head) {\n    helper(head);\n    return head;\n}\n\nprivate Node helper(Node head) {\n    Node cur = head, pre = head;\n    while(cur != null) {\n        if(cur.child == null) {\n            pre = cur;\n            cur = cur.next;\n        } else {\n            Node tmp = cur.next;\n            Node child = helper(cur.child);\n            cur.next = cur.child;\n            cur.child.prev = cur;\n            cur.child = null;\n            child.next = tmp;\n            if(tmp != null) tmp.prev = child;\n            pre = child;\n            cur = tmp;\n        }\n    }\n    return pre;\n}\n```
[]