2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_430.java 1.94 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.learn.linked_list;
/**
* 430. Flatten a Multilevel Doubly Linked List
* <p>
* 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.
* <p>
* 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.
* <p>
* Example:
* <p>
* Input:
* <p>
* 1---2---3---4---5---6--NULL
* |
* 7---8---9---10--NULL
* |
* 11--12--NULL
* <p>
* Output:
* <p>
* 1-2-3-7-8-11-12-9-10-4-5-6-NULL
*/
public class _430 {
public Node flatten(Node head) {
if (head == null) return head;
// Pointer
Node p = head;
while (p != null) {
/* CASE 1: if no child, proceed */
if (p.child == null) {
p = p.next;
continue;
}
/* CASE 2: got child, find the tail of the child and link it to p.next */
Node temp = p.child;
// Find the tail of the child
while (temp.next != null)
temp = temp.next;
// Connect tail with p.next, if it is not null
temp.next = p.next;
if (p.next != null) p.next.prev = temp;
// Connect p with p.child, and remove p.child
p.next = p.child;
p.child.prev = p;
p.child = null;
}
return head;
}
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;
}
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助