O(n) Solution in one function. TypeScript/JavaScript
Intuition\nSolution in one function\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nflatten function that recursively flattens a li
# Intuition\nSolution in one function\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n`flatten` function that recursively flattens a linked list. Has a `next` parameter to link the flattened child list to the next node of the parent list, null if the parent list is missing.\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfunction flatten(head: Node | null, next: Node = null): Node | null {\n    if(!head) return head\n    \n    let current = head\n    while(current){\n        if(current.child){\n            current.next = flatten(current.child, current.next)\n            current.next.prev = current\n            current.child = null\n        }\n        \n        if(!current.next && next){\n            current.next = next\n            next.prev = current\n            break\n        }\n        else\n            current = current.next\n    }\n       \n    \n    return head\n};\n```
['TypeScript']