The TransCompilator

Solved LeetCode program · 315 of 1,000

#0430 · Flatten a Multilevel Doubly Linked List

Medium Depth-First Search chunk-07.md

Original English source

Keep a shared values holding 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12. Keep a shared nexts holding 1, 2, 3, 4, 5, -1, 7, 8, 9, -1, 11, and -1. Keep a shared childs holding -1, -1, 6, -1, -1, -1, -1, 10, -1, -1, -1, and -1. Keep a shared stack as an empty list of whole numbers. Keep a shared cursor of 0.
To unwind from marker (a whole number). If marker is less than 0, return. The depth is the last position of stack. If depth is less than 0, set cursor to -1, return. Set cursor to item depth of stack. Remove the last item from stack.

The result is an empty list of whole numbers. Set cursor to 0.
Repeat while cursor is at least 0.
Place cursor into result. The spot is cursor. The kid is item spot of childs. The follow is item spot of nexts. If kid is at least 0 and follow is at least 0, Place follow into stack. If kid is at least 0, set cursor to kid. If kid is less than 0 and follow is at least 0, set cursor to follow. If kid is less than 0 and follow is less than 0, unwind from 0.

The line is empty text.
For each node in result.
If the length of line is more than 0, Place "," into line. Place format whole number item node of values as text into line.

Show line.

Open in Playground Browse all 1,000 problems

Expected stdout

1,2,3,7,8,11,12,9,10,4,5,6