Skip to content

Commit c58a143

Browse files
add 430
1 parent f82c7fe commit c58a143

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ _If you like this project, please leave me a star._ ★
850850
|435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | |Medium| Greedy
851851
|434|[Number of Segments in a String](https://leetcode.com/problems/number-of-segments-in-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_434.java)| |Easy|
852852
|432|[All O`one Data Structure](https://leetcode.com/problems/all-oone-data-structure/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_432.java)| |Hard| Design
853+
|430|[Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_430.java)| |Medium|LinkedList, DFS, Doubly-Linked List
853854
|429|[N-ary Tree Level Order Traversal](https://leetcode.com/problems/n-ary-tree-level-order-traversal/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_429.java)| |Easy| BFS, Tree
854855
|425|[Word Squares](https://leetcode.com/problems/word-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_425.java)| |Hard| Trie, Backtracking, Recursion
855856
|424|[Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_424.java)| | Medium| Sliding Window
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions;
2+
3+
public class _430 {
4+
public static class Solution1 {
5+
/**
6+
* credit: https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/solution/
7+
*/
8+
public Node flatten(Node head) {
9+
if (head == null) {
10+
return null;
11+
}
12+
Node pre = new Node(-1, null, head, null);
13+
dfs(pre, head);
14+
pre.next.prev = null;
15+
return pre.next;
16+
}
17+
18+
private Node dfs(Node prev, Node curr) {
19+
if (curr == null) {
20+
return prev;
21+
}
22+
curr.prev = prev;
23+
prev.next = curr;
24+
25+
Node next = curr.next;
26+
Node tail = dfs(curr, curr.child);
27+
curr.child = null;
28+
return dfs(tail, next);
29+
}
30+
}
31+
32+
public static class Node {
33+
public int val;
34+
public Node prev;
35+
public Node next;
36+
public Node child;
37+
38+
public Node(int val, Node prev, Node next, Node child) {
39+
this.val = val;
40+
this.prev = prev;
41+
this.next = next;
42+
this.child = child;
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)