From 18a808c4d10fef55124782be145ad074c36097ed Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Wed, 4 Nov 2020 18:32:06 -0700 Subject: [PATCH 1/3] added solution for 430 --- .../common/classes/DoublyLinkedNode.java | 15 +++++++ .../java/com/fishercoder/solutions/_430.java | 40 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/com/fishercoder/common/classes/DoublyLinkedNode.java create mode 100644 src/main/java/com/fishercoder/solutions/_430.java diff --git a/src/main/java/com/fishercoder/common/classes/DoublyLinkedNode.java b/src/main/java/com/fishercoder/common/classes/DoublyLinkedNode.java new file mode 100644 index 0000000000..169e9a53c9 --- /dev/null +++ b/src/main/java/com/fishercoder/common/classes/DoublyLinkedNode.java @@ -0,0 +1,15 @@ +package com.fishercoder.common.classes; + +public class DoublyLinkedNode { + public int val; + public DoublyLinkedNode next; + public DoublyLinkedNode prev; + public DoublyLinkedNode child; + public DoublyLinkedNode(int i) { + this.val = i; + } + + public int val() { + return val; + } +} diff --git a/src/main/java/com/fishercoder/solutions/_430.java b/src/main/java/com/fishercoder/solutions/_430.java new file mode 100644 index 0000000000..6a345542fa --- /dev/null +++ b/src/main/java/com/fishercoder/solutions/_430.java @@ -0,0 +1,40 @@ +package com.fishercoder.solutions; + +import com.fishercoder.common.classes.DoublyLinkedNode; + +/** + * Idea is to implement a recursive strategy by calling the recursiveFlatten() method, recursively on the child node of the parent Node + * + */ +public class _430 { + public static class Solution{ + private DoublyLinkedNode recursiveFlatten(DoublyLinkedNode head){ + DoublyLinkedNode current = head, tail = head; + while(current != null){ + DoublyLinkedNode child = current.child; + DoublyLinkedNode next = current.next; + if(child != null){ + DoublyLinkedNode output = recursiveFlatten(child); + output.next = next; + if(next != null) + next.prev = output; + current.next = child; + child.prev = current; + current.child = null; + } + else{ + current = next; + } + if(current != null){ + tail = current; + } + } + return tail; + } + public DoublyLinkedNode flatten(DoublyLinkedNode head) { + if(head != null) + recursiveFlatten(head); + return head; + } + } +} From c2e29c85bbf1a14ed4a8dad69745de0dc7ac7132 Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Wed, 4 Nov 2020 18:34:26 -0700 Subject: [PATCH 2/3] updated readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fe370cdc58..17323308fb 100644 --- a/README.md +++ b/README.md @@ -622,6 +622,7 @@ _If you like this project, please leave me a star._ ★ |435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | |Medium| Greedy |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| |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 +|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| Doubly Linked List |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 |425|[Word Squares](https://leetcode.com/problems/word-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_425.java)| |Hard| Trie, Backtracking, Recursion |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 From 7af08a8a8ce234efbd20443ec3686c2e6377183a Mon Sep 17 00:00:00 2001 From: ashmichheda Date: Wed, 4 Nov 2020 18:35:22 -0700 Subject: [PATCH 3/3] updated readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 17323308fb..7eec8af0d4 100644 --- a/README.md +++ b/README.md @@ -622,7 +622,7 @@ _If you like this project, please leave me a star._ ★ |435|[Non-overlapping Intervals](https://leetcode.com/problems/non-overlapping-intervals/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_435.java) | |Medium| Greedy |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| |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 -|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| Doubly Linked List +|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| Doubly Linked List, Recursion |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 |425|[Word Squares](https://leetcode.com/problems/word-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_425.java)| |Hard| Trie, Backtracking, Recursion |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