[C++] Simple 5 line recursive solution (with diagram)
C++\nNode* flatten(Node* head, Node* rest = nullptr) {\n  if (!head) return rest;\n  head->next = flatten(head->child, flatten(head->next, rest));\n  if (head->
```C++\nNode* flatten(Node* head, Node* rest = nullptr) {\n  if (!head) return rest;\n  head->next = flatten(head->child, flatten(head->next, rest));\n  if (head->next) head->next->prev = head;\n  head->child = nullptr;\n  return head;\n}\n```\n\nThis function modifies the structure in place. It\'s not the fastest implementation out there, but I love short recursive algorithms, and I thought this was rather nice.\n\nThe trick to make this work is to add a second parameter to the function signature. A call to `flatten(head, rest)` will flatten `head` _and_ concatenate `rest` to the end of it. That allows our recursive definition:\n\n```C++\nhead->next = flatten(head->child, flatten(head->next, rest));\n```\n\n![image](https://assets.leetcode.com/users/mhelvens/image_1563720620.png)\n\n(The first line of code is a simple base-case. The third and fourth lines are just pointer-cleanup.)\n\nWhat we\'re passing to `rest` is an already flattened `head->next`, in order to concatenate it to the end of a flattened `head->child`.
['Recursion', 'C']