资讯专栏INFORMATION COLUMN

[LeetCode] 430. Flatten a Multilevel Doubly Linked

curried / 2574人阅读

摘要:

Problem

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:

Input:
 1---2---3---4---5---6--NULL
         |
         7---8---9---10--NULL
             |
             11--12--NULL

Output:
1-2-3-7-8-11-12-9-10-4-5-6-NULL

Note:

/*
// Definition for a Node.
class Node {
    public int val;
    public Node prev;
    public Node next;
    public Node child;

    public Node() {}

    public Node(int _val,Node _prev,Node _next,Node _child) {
        val = _val;
        prev = _prev;
        next = _next;
        child = _child;
    }
};
*/
Solution - Recursive
class Solution {
    public Node flatten(Node head) {
        Node dummy = head;
        while (head != null) {
            Node next = head.next;
            
            if (head.child != null) {
                Node child = flatten(head.child);
                head.child = null;
                head.next = child;
                child.prev = head;
                while (head.next != null) head = head.next;
            } 
            
            if (next != null) {
                head.next = next;
                next.prev = head;
            }
            head = head.next;
        }
        return dummy;
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72409.html

相关文章

  • leetcode430. Flatten a Multilevel Doubly Linked Li

    摘要:步骤如下代码如下思路二循环上面的思路同样可以通过循环的方式来解决。基本步骤如下代码如下思路减少遍历次数之前的两种思路,都会出现大量的重复遍历,重复遍历和叶子节点的深度成正相关,可以想方法将重复遍历的次数减少。 题目要求 You are given a doubly linked list which in addition to the next and previous pointe...

    gxyz 评论0 收藏0
  • LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly

    摘要:您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。扁平化列表,使所有结点出现在单级双链表中。 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。 扁平化列表,使所有结点出现在单级双链表中。您将获得列表第一级的头部。 Yo...

    sugarmo 评论0 收藏0
  • LeetCode 430:扁平化多级双向链表 Flatten a Multilevel Doubly

    摘要:您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。扁平化列表,使所有结点出现在单级双链表中。 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。 扁平化列表,使所有结点出现在单级双链表中。您将获得列表第一级的头部。 Yo...

    dabai 评论0 收藏0
  • [LeetCode] 426. Convert BST to Sorted Doubly Linke

    Problem Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Lets take the foll...

    MartinDai 评论0 收藏0
  • [LintCode/LeetCode] Flatten Binary Tree to Linked

    Problem Flatten a binary tree to a fake linked list in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode. Example 1 1 ...

    TNFE 评论0 收藏0

发表评论

0条评论

curried

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<