Skip to content

Commit

Permalink
add 430 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 23, 2024
1 parent 3975991 commit b05b2ce
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@
445| [Add Two Numbers II](https://leetcode.com/problems/add-two-numbers-ii/)| [Python](./leetcode_python/Linked_list/add-two-numbers-ii.py) | _O(m + n)_ | _O(m + n)_| Medium |`trick`, linked list, string,`good basic`, `amazon`| AGAIN*** (3)
725 | [Split Linked List in Parts](https://leetcode.com/problems/split-linked-list-in-parts/) | [Python](./leetcode_python/Linked_list/split-linked-list-in-parts.py) | _O(n + k)_ | _O(1)_ | Medium |mod, split linked list, linked list, good trick,`amazon`| AGAIN************ (6) (again)
817 | [Linked List Components](https://leetcode.com/problems/linked-list-components/) | [Python](./leetcode_python/Linked_list/linked-list-components.py) | _O(m + n)_ | _O(m)_ | Medium || OK*
430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/) | [Python](./leetcode_python/Linked_list/flatten_a_multilevel_doubly_linked_list.py) | || Medium |`good trick`,`doubly linked list`,`AGAIN`,`dfs`, `fb`| AGAIN********* (4)
430 | [Flatten a Multilevel Doubly Linked List](https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/) | [Python](./leetcode_python/Linked_list/flatten_a_multilevel_doubly_linked_list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/FlattenMultilevelDoublyLinkedList.java) | || Medium |`good trick`,`doubly linked list`,`AGAIN`,`dfs`, `fb`, google| AGAIN********* (4)
707| [Design Linked List](https://leetcode.com/problems/design-linked-list/) | [Python](./leetcode_python/Linked_list/design_linked_list.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/LinkedList/DesignLinkedList.java) | _O(n)_ | _O(h)_ | Medium|linked list basic OP| AGAIN (1)
708| [Insert into a Cyclic Sorted List](https://leetcode.com/problems/insert-into-a-sorted-circular-linked-list) | [Python](./leetcode_python/Linked_list/insert_into_a_cyclic_sorted_sorted_linked_list.py) | _O(n)_ | _O(h)_ | Medium|`AGAIN`,`cyclic linked list`,`good trick`, `google`, `amazon`, `fb` | AGAIN******** (4)

Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240923: 363
20240923: 363,430
20240922: 1032,844,1011
20240921: 947
20240918: 753
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
2024-11-17 -> ['363']
2024-11-17 -> ['363,430']
2024-11-16 -> ['1032,844,1011']
2024-11-15 -> ['947']
2024-11-12 -> ['753']
2024-11-11 -> ['727']
2024-11-04 -> ['659']
2024-11-03 -> ['801,552']
2024-11-02 -> ['1057,1066,1110']
2024-10-27 -> ['363']
2024-10-27 -> ['363,430']
2024-10-26 -> ['1032,844,1011']
2024-10-25 -> ['947', '1110, 1055']
2024-10-22 -> ['753']
2024-10-21 -> ['727']
2024-10-18 -> ['359,1057,1055(todo)']
2024-10-14 -> ['363', '659']
2024-10-14 -> ['363,430', '659']
2024-10-13 -> ['1032,844,1011', '801,552']
2024-10-12 -> ['947', '1057,1066,1110']
2024-10-09 -> ['753']
2024-10-08 -> ['727']
2024-10-06 -> ['363']
2024-10-06 -> ['363,430']
2024-10-05 -> ['1032,844,1011']
2024-10-04 -> ['947', '315', '1110, 1055']
2024-10-01 -> ['363', '753', '659']
2024-10-01 -> ['363,430', '753', '659']
2024-09-30 -> ['1032,844,1011', '727', '801,552']
2024-09-29 -> ['947', '1057,1066,1110']
2024-09-28 -> ['363']
2024-09-28 -> ['363,430']
2024-09-27 -> ['1032,844,1011', '359,1057,1055(todo)']
2024-09-26 -> ['363', '947', '753']
2024-09-25 -> ['363', '1032,844,1011', '727']
2024-09-24 -> ['363', '1032,844,1011', '947']
2024-09-26 -> ['363,430', '947', '753']
2024-09-25 -> ['363,430', '1032,844,1011', '727']
2024-09-24 -> ['363,430', '1032,844,1011', '947']
2024-09-23 -> ['1032,844,1011', '947', '753', '659']
2024-09-22 -> ['947', '727', '801,552']
2024-09-21 -> ['753', '1057,1066,1110', '1110, 1055']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package LeetCodeJava.LinkedList;

// https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/description/

import java.util.LinkedList;
import java.util.Queue;

/**
* 430. Flatten a Multilevel Doubly Linked List
* Solved
* Medium
* Topics
* Companies
* You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. 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>
* Given the head of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr be a node with a child list. The nodes in the child list should appear after curr and before curr.next in the flattened list.
* <p>
* Return the head of the flattened list. The nodes in the list must have all of their child pointers set to null.
* <p>
* <p>
* <p>
* Example 1:
* <p>
* <p>
* 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 shown.
* After flattening the multilevel linked list it becomes:
* <p>
* Example 2:
* <p>
* <p>
* Input: head = [1,2,null,3]
* Output: [1,3,2]
* Explanation: The multilevel linked list in the input is shown.
* After flattening the multilevel linked list it becomes:
* <p>
* Example 3:
* <p>
* Input: head = []
* Output: []
* Explanation: There could be empty list in the input.
* <p>
* <p>
* Constraints:
* <p>
* The number of Nodes will not exceed 1000.
* 1 <= Node.val <= 105
* <p>
* <p>
* How the multilevel linked list is represented in test cases:
* <p>
* We use the multilevel linked list from Example 1 above:
* <p>
* 1---2---3---4---5---6--NULL
* |
* 7---8---9---10--NULL
* |
* 11--12--NULL
* The serialization of each level is as follows:
* <p>
* [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:
* <p>
* [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:
* <p>
* [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
*/
public class FlattenMultilevelDoublyLinkedList {

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

// V0
// TODO : implement
// public Node flatten(Node head) {
//
// }

// V1
// https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/solutions/5667355/easy-java-solution/
class Node {
public int val;
public Node prev;
public Node next;
public Node child;
};

private Queue<Node> store = new LinkedList<>();

public void helper(Node head) {
Node temp;
while (head != null) {
temp = head.next;
head.next = null;
head.prev = null;
store.offer(head);
if (head.child != null)
helper(head.child);
head.child = null;
head = temp;
}
}

public Node flatten_1(Node head) {
helper(head);
if (store.peek() == null)
return head;
Node retval = store.poll();
Node first = retval;
while (store.peek() != null) {
Node second = store.poll();
first.next = second;
second.prev = first;
first = second;
}
first.next = null;
return retval;
}


// V2
// IDEA : LINKED LIST + CUSTOM CLASS
// https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/solutions/4031041/single-pass-solution-using-custom-class/
class lc430Helper {
Node head;
Node tail;
}

public Node flatten_2(Node head) {
return util1(head).head;
}

public lc430Helper util1(Node head) {
Node dummy = new Node();
dummy.next = head;
Node temp = dummy;
while (temp.next != null) {
temp = temp.next;
if (temp.child == null) {
continue;
} else {
lc430Helper bottom = util1(temp.child);
Node temp2 = temp.next;
temp.child = null;
temp.next = bottom.head;
bottom.head.prev = temp;
bottom.tail.next = temp2;
if (temp2 != null) {
temp2.prev = bottom.tail;
}
temp = bottom.tail;
}
}
lc430Helper ans = new lc430Helper();
dummy.next = null;
ans.head = head;
ans.tail = temp;
return ans;
}

// V3
// https://leetcode.com/problems/flatten-a-multilevel-doubly-linked-list/solutions/5328452/easy-to-understand-best-solution/
public Node flatten_3(Node head) {
Node temp = head;
while(temp != null){
Node list1Tail = temp;
Node list3Head = temp.next;
// if the node has child node then append all its node in between

if(temp.child != null){
// we are assuming that recursion will give us flatten output, so we just need to adjust the pointers
Node list2Head = flatten_3(temp.child);

// find list2 tail
Node list2Tail = list2Head;
while(list2Tail.next != null){
list2Tail = list2Tail.next;
}

// attach the lists
list1Tail.next = list2Head;
list2Head.prev = list1Tail;
list2Tail.next = list3Head;
if(list3Head != null)
list3Head.prev = list2Tail;
temp.child = null;
}
temp = temp.next;
}
return head;
}

}

0 comments on commit b05b2ce

Please sign in to comment.