2 Different Ways, Iterative & Recursive, With Comments
1. Iterative using Stack\n\n\npublic class Solution {    \n    public Node Flatten(Node head) { \n        var current = head;\n        var stack = new Stack<Nod
**1. Iterative using Stack**\n\n```\npublic class Solution {    \n    public Node Flatten(Node head) { \n        var current = head;\n        var stack = new Stack<Node>();\n        \n        // Loop through nodes\n        while (current != null) {\n            \n            // Check to see if node has child\n            if (current.child != null) {\n                // If current node has a next node, save to stack \n                // so we can reconnect it to the tail \n                // of the child node later\n                if (current.next != null) {\n                    stack.Push(current.next);\n                }\n                \n                // Set the next node as the child,\n                // we will now iterate down this path\n                current.next = current.child;            \n                \n                // Set the previous node as the current\n                current.next.prev = current;\n                \n                // Set child to null\n                current.child = null;  \n                \n            } else if (current.next == null) {\n                // Reconnect node at the top of the \n                // stack to the tail child node\n                if (stack.Count > 0) {\n                    // Set the next node as the reconnected node,\n                    // we will now iterate down this path\n                    current.next = stack.Pop();\n                    current.next.prev = current;                    \n                }\n            }\n            current = current.next;\n        }\n        \n        return head;\n    }\n}\n```\n\n\n**2. Recursive**\n\n```\npublic class Solution {    \n    public Node Flatten(Node head) { \n        Flatten(head, null);        \n        return head;\n    }\n    \n    private Node Flatten(Node current, Node previous) {\n        if (current == null) {\n            return previous;\n        }\n        \n        // If previous node exists, set the next and previous values\n        if (previous != null) {\n            previous.next = current;\n            current.prev = previous;\n        }\n        \n        // Save the next node so we can reconnect it to the tail \n        // of the child node later\n        var next = current.next;\n        \n        // Traverse down child path. \n        // If children exist, this returns the last child for the current node\n        var tail = Flatten(current.child, current);\n        \n        // Child path has been explored, set to null\n        current.child = null;\n        \n        // Reconnect next node to the tail child node\n        return Flatten(next, tail);        \n    }\n}\n```
[]