You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.
Given the
head
of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr
be a node with a child list. The nodes in the child list should appear after curr
and before curr.next
in the flattened list.Return the
head
of the flattened list. The nodes in the list must have all of their child pointers set to null
.

Constraints:
- The number of Nodes will not exceed
1000
.
1 <= Node.val <= 10
5
How the multilevel linked list is represented in test cases:
We use the multilevel linked list from Example 1 above:
1---2---3---4---5---6--NULL | 7---8---9---10--NULL | 11--12--NULL
The serialization of each level is as follows:
[1,2,3,4,5,6,null] [7,8,9,10,null] [11,12,null]
To serialize all levels together, we will add nulls in each level to signify no node connects to the upper node of the previous level. The serialization becomes:
[1, 2, 3, 4, 5, 6, null] | [null, null, 7, 8, 9, 10, null] | [ null, 11, 12, null]
Merging the serialization of each level and removing trailing nulls we obtain:
[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
풀이
재영짱짱1234
첫 번째 풀이
Runtime: 112 ms, faster than 10.93%
/** * // 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) { const dfs = (node, next = null) => { while (node) { if (node.child) { const cachedNext = node.next; const nextHead = node.child; dfs(node.child, cachedNext ?? next); node.child = null; node.next = nextHead; nextHead.prev = node; node = cachedNext; continue; } if (!node.next) { if (next) { node.next = next; next.prev = node; } return; } node = node.next; } } dfs(head); return head; };
리팩토링
특정 값을 반복하여 사용함으로써 객체 키를 통한 주소 접근 최소화
Runtime: 60 ms, faster than 94.62%
/** * // 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) { const dfs = (node, next = null) => { while (node) { const cachedNext = node.next; const nodeChild = node.child; if (nodeChild) { dfs(nodeChild, cachedNext ?? next); node.child = null; node.next = nodeChild; nodeChild.prev = node; node = cachedNext; continue; } if (!cachedNext) { if (next) { node.next = next; next.prev = node; } return; } node = cachedNext; } } dfs(head); return head; };
은찬
var flatten = function(head) { const arr = []; let current = head; let answer; const dfs = (node) => { arr.push(new Node(node.val)); if(node.child !== null){ dfs(node.child); } if(node.next !== null){ dfs(node.next); } } if(!head){ return head; } dfs(current); answer = arr[0]; for(let i = 0; i < arr.length; i++){ if(i < arr.length - 1){ arr[i].next = arr[i + 1]; } if(i > 0){ arr[i].prev = arr[i - 1]; } } return answer; };
효성
var flatten = function(head) { let prevNode = null; let curNode = null; const dfs = (node) => { while(node) { prevNode = curNode; curNode = node; const nextNode = node.next; if(prevNode) { prevNode.next = curNode; prevNode.child = null; } curNode.prev = prevNode; if(node.child) { dfs(node.child); } node = nextNode; } } dfs(head); return head; };