Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created July 10, 2020 08:09
Show Gist options
  • Save RP-3/e7a9d45c644b67b4be6be90198fd675e to your computer and use it in GitHub Desktop.
Save RP-3/e7a9d45c644b67b4be6be90198fd675e to your computer and use it in GitHub Desktop.
/**
* // 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