Search code examples
javalinked-listtime-complexitydoubly-linked-list

Time complexity of this code that flattens a multilevel doubly linked list


I was solving this LeetCode question - Flatten a Multilevel Doubly Linked List:

You are given a doubly linked list, which contains nodes that have a next pointer, a previous pointer, and an additional child pointer. This child pointer may or may not point to a separate doubly linked list, also containing these special nodes. These child lists may have one or more children of their own, and so on, to produce a multilevel data structure as shown in the example below.

Given the head of the first level of the list, flatten the list so that all the nodes appear in a single-level, doubly linked list. Let curr be a node with a child list. The nodes in the child list should appear after curr and before curr.next in the flattened list.

Return the head of the flattened list. The nodes in the list must have all of their child pointers set to null.

Example 1

enter image description here

Input: head = [1,2,3,4,5,6,null,null,null,7,8,9,10,null,null,11,12]
Output: [1,2,3,7,8,11,12,9,10,4,5,6]
Explanation: The multilevel linked list in the input is shown.
After flattening the multilevel linked list it becomes: enter image description here

I have come up with below solution:

class Solution {
    public Node flatten(Node head) {
        if(head == null) {
            return head;
        }
        
        Node curr = head;
        Node prev = curr.prev;
        Node next = curr.next;

        while(curr != null) {
            if(curr.child != null) {
                Node child = flatten(curr.child);
                // Get the last node of the child list
                while(child.next != null) {
                    child = child.next;
                }
                Node tail = child;
                curr.next = curr.child;
                curr.child.prev = curr;
                tail.next = next;
                if(next != null) {
                    next.prev = tail;
                }
                curr.child = null;
            }
            // Move to next node
            prev = next != null ? next.prev : curr;
            curr = next;
            next = next != null ? next.next : null;
        }

        return head;
    }
}

Question

What would be the worst case time complexity of my solution?

Since I'm iterating through children to get the tail, I think it could get bad if all nodes had children, but I don't see how to derive the time complexity.

For example:

1 <-> 2 <-> 3 <-> 4
|     |     |     |
5     6     7     8
|     |     |     |
9     10    11    12
|     |     |     |
13    14    15    16

1 <-> 2 <-> 3 <-> 4 ...
|     |     |     |
5     6     7     8 <-> 17 <-> 19
|     |     |     |      |      |
9     10    11    12    18     20 ...
|     |     |     |      .      .
13    14    15    16     .      .

Solution

  • An example worst case in terms of 𝑛 (number of nodes) is when all nodes are in a vertical dependency

    1
    |
    2
    |
    3
    |
    4
    |
    5
    |
    6
    

    Here, none of the nodes has a next node.

    Each time a recursive call unwinds, the new bottom level will have one more node. That bottom level is then traversed again. We can thus say that the statement child = child.next; is executed this many times:

          0 + 1 + 2 + ... + 𝑛−1 = O(𝑛²)

    You could adapt the algorithm so that your function returns the tail node of the child level. The top level call would ignore this return value from recursion and still return the head. That way you don't have to traverse the child list again.

    Here is the adapted code, which has a O(𝑛) time complexity now:

    class Solution {
        public Node flatten(Node head) {
            flattenAndReturnTail(head);
            return head;
        }
    
        public Node flattenAndReturnTail(Node curr) {
            Node tail = null;
            while (curr != null) {
                tail = curr;
                Node next = curr.next;
                if (curr.child != null) {
                    tail = flattenAndReturnTail(curr.child);
                    // Link child's tail
                    tail.next = next;
                    if (next != null) {
                        next.prev = tail;
                    }
                    // Link child's head (i.e. curr.child)
                    curr.next = curr.child;
                    curr.child.prev = curr;
                    curr.child = null;
                }
                curr = next;
            }
            return tail;
        }
    }