Created
July 10, 2020 08:09
-
-
Save RP-3/e7a9d45c644b67b4be6be90198fd675e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* // Definition for a Node. | |
* function Node(val,prev,next,child) { | |
* this.val = val; | |
* this.prev = prev; | |
* this.next = next; | |
* this.child = child; | |
* }; | |
*/ | |
/** | |
* @param {Node} head | |
* @return {Node} | |
*/ | |
var flatten = function(head) { | |
if(!head) return head; | |
const dfs = (node) => { | |
if(!node.child){ | |
const tail = dfs(node.child); | |
tail.next = node.next; | |
if(node.next) node.next.prev = tail; | |
node.next = node.child; | |
node.child.prev = node; | |
node.child = null; | |
} | |
return node.next ? dfs(node.next) : node; | |
}; | |
dfs(head); | |
return head; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment