TC: O(n), SC: O(1) No stack, No recursion Simple One Pass Python Solution
\ndef flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n        if not head: return\n        curr = head\n        while curr:\n            if curr
```\ndef flatten(self, head: \'Optional[Node]\') -> \'Optional[Node]\':\n        if not head: return\n        curr = head\n        while curr:\n            if curr.child:\n                childNode = curr.child\n                childNode.prev = curr      #setting previous pointer\n                \n                while childNode.next:\n                    childNode = childNode.next\n                    \n                childNode.next = curr.next\n                \n                if childNode.next: childNode.next.prev = childNode\n                    \n                curr.next = curr.child\n                curr.child = None\n                \n            curr = curr.next\n            \n        return head\n```\nThe traversal technique used here is known as Morris Traversal.\nMorris traversal is generally used to traverse binary trees, but if you give it some thought, the doubly linked list given in this problem is just like a Binary Tree with previous pointers.\nChild pointer and next pointer can be thought of as left pointer and right pointer of a tree respectively.\nOnce we look at the problem like this it becomes similar to flattening a binary tree to a linked list, just with two extra steps to set the previous pointers.\n![image](https://assets.leetcode.com/users/images/552732c0-3a91-46a0-8db7-cb6eacf49282_1650180767.6820588.jpeg)\n\nIf you remove the two extra steps where you set the previous pointers, you get a solution to this problem: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/\n```\ndef flatten(self, root: Optional[TreeNode]) -> None:\n        if root == None: return\n        curr = root\n        while curr:\n            if curr.left:\n                childNode = curr.left\n                while childNode.right:\n                    childNode = childNode.right\n                childNode.right = curr.right\n                curr.right = curr.left\n                curr.left = None\n            curr = curr.right\n```\n\n
['Python', 'Python3']