LeetCode 430. Flatten a Multilevel Doubly Linked

简介: 使用循环,从头开始遍历,将子链看成一个整体,将整体向上移动一层。继续向后遍历,如果遇到子链,将其看作一层,将其整体向上移动,如此循环下去,直至结束。

v2-9dcb6a5ff4a03f24ca829cf084b06518_1440w.jpg

Description



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.

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.


Example 1:


Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
Explanation:
The multilevel linked list in the input is as follows:



image.png


After flattening the multilevel linked list it becomes:


v2-00a6cd1eda35da1292f936ff4decb752_720w.png


Example 2:


Input: head = [1,2,null,3]
Output: [1,3,2]
Explanation:


The input multilevel linked list is as follows:
  1---2---NULL
  |
  3---NULL


Example 3:


Input: head = []
Output: []


How multilevel linked list is represented in test case:


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]


思路



  • 使用循环,从头开始遍历,将子链看成一个整体,将整体向上移动一层。
  • 继续向后遍历,如果遇到子链,将其看作一层,将其整体向上移动,如此循环下去,直至结束。


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-12-29 10:57:03
# @Last Modified by:   何睿
# @Last Modified time: 2019-12-29 11:05:17
class Node:
    def __init__(self, val, prev, next, child):
        self.val = val
        self.prev = prev
        self.next = next
        self.child = child
class Solution:
    def flatten(self, head: 'Node') -> 'Node':
        current = head
        while current:
            if current.child:
                _next = current.next  # 当前节点的后一个节点
                last = current.child  # 当前节点的自节点
                while last.next:
                    last = last.next  # 如果有子节点,找到子节点的最后一个节点
                current.next = current.child
                current.next.prev = current  # 将自节点向上提高一层
                current.child = None
                last.next = _next
                if _next:
                    _next.prev = last
            current = current.next
        return head

源代码文件在 这里


目录
相关文章
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
45 1
LeetCode 341. Flatten Nested List Iterator
给定一个嵌套的整型列表。设计一个迭代器,使其能够遍历这个整型列表中的所有整数。 列表中的项或者为一个整数,或者是另一个列表。
69 0
LeetCode 341. Flatten Nested List Iterator
LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly Linked List
您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。 扁平化列表,使所有结点出现在单级双链表中。
852 0
|
C++
[LeetCode] Flatten 2D Vector
Problem Description:   Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] By callin...
897 0
[LeetCode] Flatten Binary Tree to Linked List
This problem seems to be tricky at first glance. However, if you know Morris traversal, it is just the preorder case of Morris traversal and the code is really short.
830 0
LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
要求:Given a binary tree, flatten it to a linked list in-place.将二叉树转化为平坦序列的树。比如: 结题思路: 该题有个提示,转化后的树的序列正好是二叉树前序遍历所得到的序列,所以,该题第一个思路就是利用前序遍历的方式来做。
857 0
|
2月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行