代码拉取完成,页面将自动刷新
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;
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。