Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 0 ms, faster than 100.00% of Java online submissions for Flatten a Multilevel Doubly Linked List.
- class Solution {
- public Node flatten(Node head) {
- flatten(head, null);
- return head;
- }
- private Node flatten(Node head, Node rest) {
- if (head == null) return rest;
- Node c = head;
- while (c.next != null && c.child == null) c = c.next;
- c.next = flatten(c.child, flatten(c.next, rest));
- if (c.next != null) c.next.prev = c;
- c.child = null;
- return head;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement