Brian Jenney
1 min readFeb 23, 2020

--

Well, leetcode doesn’t really like my answer but I think the overall approach is correct:

var flatten = function(head) {

let list = head;

while(list){
if(list.child){
// save the one that would have been next
let temp = list.next;
list.next = flatten(list.child);
list.next.prev = temp;

// get to the end of our flattened list that
// was appended and then add our original 'next'

while(list.next){
list = list.next
}

list.next = temp;
temp.prev = list;
}

list = list.next;

}

return head
};

--

--

Brian Jenney
Brian Jenney

Written by Brian Jenney

full-stackish developer, late bloomer coder and power google user and owner of Parsity.io

No responses yet