# LeetCode: Flatten a Multilevel Doubly Linked List Solution

Naming variables is also important

## Approach

Iterative way

If a node has a child:

• point node's
next
to child, child's
prev
to node
• iterate child to its tail, then point tail's next to original node's next, original node's next's prev to tail

But I think variables naming for better readability in this task is important also, for our future self

## Implementation

```.css-ds3kc{display:table-row;}.css-1t8atru{display:table-cell;opacity:0.5;padding-right:var(--chakra-space-6);-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;text-align:right;}1.css-2qghsv{display:table-cell;}var flatten = function (head) {2  for (let node = head; node; node = node.next) {3    if (!node.child) continue4
5    let child = node.child6    let next = node.next7
8    node.next = child9    child.prev = node10    node.child = null11
12    while (child.next) child = child.next13    child.next = next14    if (next) next.prev = child15  }16  return head17}```

leetcode

## Next Post

LeetCode: Rotate List

Aug 22, 2021

Should draw for better imagination of how it works