Skip to content

Add solution for 430 #125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1421,6 +1421,7 @@ _If you like this project, please leave me a star._ ★
| 2 |[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| [Solution](../master/src/main/java/com/fishercoder/solutions/_2.java) | | Medium | LinkedList
| 1 |[Two Sum](https://leetcode.com/problems/two-sum/)| [Java](../master/src/main/java/com/fishercoder/solutions/_1.java), [C++](../master/cpp/_1.cpp), [Javascript](../master/javascript/_1.js) | [:tv:](https://www.youtube.com/watch?v=kPXOr6pW8KM&t=) | Easy | HashMap


## Database

| # | Title | Solutions | Video | Difficulty | Tag
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/fishercoder/common/classes/DoublyLinkedNode.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
37 changes: 37 additions & 0 deletions src/main/java/com/fishercoder/solutions/_430.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
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 Solution1 {
/**
Expand Down Expand Up @@ -28,6 +35,36 @@ private Node dfs(Node prev, Node curr) {
return dfs(tail, next);
}
}
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;
}
}

public static class Node {
public int val;
Expand Down
Loading