DEV Community

Jaspreet singh
Jaspreet singh

Posted on

Flatten a Linked List

Problem that secretly tests whether you can recognize Merge K Sorted Lists hidden behind a different structure.


Problem Statement

We are given a linked list where:

  • next pointer connects different linked lists horizontally.
  • bottom pointer connects nodes vertically.
  • Every vertical list is already sorted.

Our task is to flatten the entire structure into a single sorted linked list using only the bottom pointer.

Example

5 -> 10 -> 19 -> 28
|     |     |     |
7     20    22    35
|           |     |
8           50    40
|
30
Enter fullscreen mode Exit fullscreen mode

Output:

5 -> 7 -> 8 -> 10 -> 19 -> 20 -> 22 -> 28 -> 30 -> 35 -> 40 -> 50
Enter fullscreen mode Exit fullscreen mode

Brute Force Intuition

The most straightforward approach is:

  1. Traverse all nodes.
  2. Store values inside an array.
  3. Sort the array.
  4. Rebuild the linked list.

How to Explain in an Interview

Since all nodes are accessible through next and bottom pointers, we can collect every value into a list, sort it, and create a new flattened linked list. Although simple, this approach doesn't utilize the fact that individual bottom lists are already sorted.

Complexity

Time Complexity  : O(N log N)
Space Complexity : O(N)
Enter fullscreen mode Exit fullscreen mode

where N is the total number of nodes.


Observation That Changes Everything

Notice something important:

Every bottom linked list is already sorted.

That means we're unnecessarily sorting data again.

Instead of:

Collect → Sort → Rebuild
Enter fullscreen mode Exit fullscreen mode

we can directly:

Merge Sorted Lists
Enter fullscreen mode Exit fullscreen mode

This suddenly becomes very similar to:

Merge K Sorted Linked Lists

And whenever you hear that phrase, a merge-based solution should immediately come to mind.


Optimal Approach

Core Idea

Flatten the linked list from right to left.

Imagine:

5 -> 10 -> 19 -> 28
Enter fullscreen mode Exit fullscreen mode

We first flatten:

19 -> 28
Enter fullscreen mode Exit fullscreen mode

Then merge the result with:

10
Enter fullscreen mode Exit fullscreen mode

And finally merge everything with:

5
Enter fullscreen mode Exit fullscreen mode

At every stage, we only merge two sorted linked lists.


Algorithm

Step 1

Flatten all lists on the right.

root.next = flatten(root.next);
Enter fullscreen mode Exit fullscreen mode

Step 2

Merge current list with the already flattened right side.

return merge(root, root.next);
Enter fullscreen mode Exit fullscreen mode

Step 3

Use standard merge logic of two sorted linked lists.

Compare values and connect nodes through bottom pointers.


Java Solution

class Solution {

    public Node flatten(Node root) {

        if (root == null || root.next == null) {
            return root;
        }

        root.next = flatten(root.next);

        return merge(root, root.next);
    }

    private Node merge(Node a, Node b) {

        Node dummy = new Node(0);
        Node current = dummy;

        while (a != null && b != null) {

            if (a.data <= b.data) {
                current.bottom = a;
                a = a.bottom;
            } else {
                current.bottom = b;
                b = b.bottom;
            }

            current = current.bottom;
            current.next = null;
        }

        if (a != null) {
            current.bottom = a;
        } else {
            current.bottom = b;
        }

        return dummy.bottom;
    }
}
Enter fullscreen mode Exit fullscreen mode

Dry Run

Input:

5 -> 10 -> 19

5 -> 7 -> 8 -> 30

10 -> 20

19 -> 22 -> 50
Enter fullscreen mode Exit fullscreen mode

First Merge

10 -> 20
Enter fullscreen mode Exit fullscreen mode

with

19 -> 22 -> 50
Enter fullscreen mode Exit fullscreen mode

Result:

10 -> 19 -> 20 -> 22 -> 50
Enter fullscreen mode Exit fullscreen mode

Second Merge

5 -> 7 -> 8 -> 30
Enter fullscreen mode Exit fullscreen mode

with

10 -> 19 -> 20 -> 22 -> 50
Enter fullscreen mode Exit fullscreen mode

Result:

5 -> 7 -> 8 -> 10 -> 19 -> 20 -> 22 -> 30 -> 50
Enter fullscreen mode Exit fullscreen mode

Flattened and sorted.


Why This Works

The recursion guarantees that:

flatten(root.next)
Enter fullscreen mode Exit fullscreen mode

always returns a sorted flattened list.

So after recursion, the problem reduces to:

Merge Two Sorted Linked Lists
Enter fullscreen mode Exit fullscreen mode

And that's a problem we've already solved many times.

The beauty of this question is not the code.

The beauty is recognizing the pattern hiding underneath.


Pattern Recognition

Whenever a problem contains:

  • Multiple sorted lists
  • Need one final sorted list
  • Merge operation available

Think:

Merge K Sorted Lists
Enter fullscreen mode Exit fullscreen mode

This question is simply that pattern wearing a Linked List disguise.


Complexity Analysis

Let:

  • N = Total Nodes
  • K = Number of vertical linked lists

Time Complexity

O(N × K)
Enter fullscreen mode Exit fullscreen mode

Space Complexity

O(K)
Enter fullscreen mode Exit fullscreen mode

Recursive stack space.


Interview Takeaway

Most candidates see a complicated linked list structure.

Strong candidates see:

"A collection of sorted linked lists waiting to be merged."

The moment you identify that pattern, the entire problem becomes a standard merge exercise.

Top comments (0)