Advertisement
ogv

Untitled

ogv
Oct 14th, 2019
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.62 KB | None | 0 0
  1. // Runtime: 0 ms, faster than 100.00% of Java online submissions for Flatten a Multilevel Doubly Linked List.
  2. class Solution {
  3.     public Node flatten(Node head) {
  4.         flatten(head, null);
  5.         return head;
  6.     }    
  7.    
  8.     private Node flatten(Node head, Node rest) {
  9.         if (head == null) return rest;
  10.        
  11.         Node c = head;                
  12.         while (c.next != null && c.child == null) c = c.next;
  13.                
  14.         c.next = flatten(c.child, flatten(c.next, rest));
  15.         if (c.next != null) c.next.prev = c;
  16.         c.child = null;
  17.        
  18.         return head;
  19.     }        
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement