|
| 1 | +# LeetCode 430. Flatten a Multilevel Doubly Linked List |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +You are given a doubly linked list which in addition to the next and previous pointers, it could have a child pointer, which may or may not point to a separate doubly linked list. 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. |
| 6 | + |
| 7 | +Flatten the list so that all the nodes appear in a single-level, doubly linked list. You are given the head of the first level of the list. |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | +Example 1: |
| 12 | + |
| 13 | +```py |
| 14 | +Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12] |
| 15 | +Output: [1,2,3,7,8,11,12,9,10,4,5,6] |
| 16 | +Explanation: |
| 17 | + |
| 18 | +The multilevel linked list in the input is as follows: |
| 19 | +``` |
| 20 | + |
| 21 | + |
| 22 | +After flattening the multilevel linked list it becomes: |
| 23 | + |
| 24 | + |
| 25 | +Example 2: |
| 26 | +```py |
| 27 | +Input: head = [1,2,null,3] |
| 28 | +Output: [1,3,2] |
| 29 | +Explanation: |
| 30 | +``` |
| 31 | +```py |
| 32 | +The input multilevel linked list is as follows: |
| 33 | + |
| 34 | + 1---2---NULL |
| 35 | + | |
| 36 | + 3---NULL |
| 37 | +``` |
| 38 | +Example 3: |
| 39 | + |
| 40 | +```py |
| 41 | +Input: head = [] |
| 42 | +Output: [] |
| 43 | +``` |
| 44 | + |
| 45 | +How multilevel linked list is represented in test case: |
| 46 | + |
| 47 | +```py |
| 48 | +We use the multilevel linked list from Example 1 above: |
| 49 | + |
| 50 | + 1---2---3---4---5---6--NULL |
| 51 | + | |
| 52 | + 7---8---9---10--NULL |
| 53 | + | |
| 54 | + 11--12--NULL |
| 55 | +The serialization of each level is as follows: |
| 56 | + |
| 57 | +[1,2,3,4,5,6,null] |
| 58 | +[7,8,9,10,null] |
| 59 | +[11,12,null] |
| 60 | +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: |
| 61 | + |
| 62 | +[1,2,3,4,5,6,null] |
| 63 | +[null,null,7,8,9,10,null] |
| 64 | +[null,11,12,null] |
| 65 | +Merging the serialization of each level and removing trailing nulls we obtain: |
| 66 | + |
| 67 | +[1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12] |
| 68 | +``` |
| 69 | + |
| 70 | +### 思路 |
| 71 | + |
| 72 | +* 使用循环,从头开始遍历,将子链看成一个整体,将整体向上移动一层。 |
| 73 | +* 继续向后遍历,如果遇到子链,将其看作一层,将其整体向上移动,如此循环下去,直至结束。 |
| 74 | + |
| 75 | +```py |
| 76 | +# -*- coding: utf-8 -*- |
| 77 | +# @Author: 何睿 |
| 78 | +# @Create Date: 2019-12-29 10:57:03 |
| 79 | +# @Last Modified by: 何睿 |
| 80 | +# @Last Modified time: 2019-12-29 11:05:17 |
| 81 | + |
| 82 | + |
| 83 | +class Node: |
| 84 | + def __init__(self, val, prev, next, child): |
| 85 | + self.val = val |
| 86 | + self.prev = prev |
| 87 | + self.next = next |
| 88 | + self.child = child |
| 89 | + |
| 90 | + |
| 91 | +class Solution: |
| 92 | + def flatten(self, head: 'Node') -> 'Node': |
| 93 | + current = head |
| 94 | + while current: |
| 95 | + if current.child: |
| 96 | + _next = current.next # 当前节点的后一个节点 |
| 97 | + last = current.child # 当前节点的自节点 |
| 98 | + while last.next: |
| 99 | + last = last.next # 如果有子节点,找到子节点的最后一个节点 |
| 100 | + current.next = current.child |
| 101 | + current.next.prev = current # 将自节点向上提高一层 |
| 102 | + current.child = None |
| 103 | + last.next = _next |
| 104 | + if _next: |
| 105 | + _next.prev = last |
| 106 | + current = current.next |
| 107 | + |
| 108 | + return head |
| 109 | +``` |
| 110 | + |
| 111 | +源代码文件在 [这里](https://github.com/ruicore/Algorithm/blob/master/LeetCode/2019-12-29-430-Flatten-a-Multilevel-Doubly-Linked-List.py) 。 |
| 112 | +©本文首发于 何睿的博客 ,欢迎转载,转载需保留 [文章来源](https://ruicore.cn/leetcode-430-flatten-a-multilevel-doubly-linked-list/) ,作者信息和本声明. |
0 commit comments