Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.97 KB | None | 0 0
  1. /*
  2. Runtime: 0 ms, faster than 100.00% of Java online submissions for Flatten a Multilevel Doubly Linked List.
  3. Memory Usage: 36.8 MB, less than 100.00% of Java online submissions for Flatten a Multilevel Doubly Linked List.
  4. */
  5. class Solution {
  6.     public Node flatten(Node head) {
  7.         Node current = head;
  8.  
  9.         while (current != null) {
  10.             if (current.child != null) {
  11.                 Node last = current.next;
  12.                 Node next = flatten(current.child);
  13.                 current.next = next;
  14.                 next.prev = current;
  15.                 current.child = null;
  16.                
  17.                 while(current.next != null) {
  18.                     current = current.next;
  19.                 }
  20.                
  21.                 if (last != null) {
  22.                     current.next = last;
  23.                     last.prev = current;
  24.                 }
  25.             }
  26.             current = current.next;
  27.         }
  28.        
  29.         return head;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement