Skip to content

Commit b3a93bd

Browse files
author
Priyanshu1303d
committed
[FEAT] Implement Flattening Multilevel LinkedList
1 parent 9484c7e commit b3a93bd

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.thealgorithms.datastructures.lists;
2+
/**
3+
* Implements an algorithm to flatten a multilevel linked list.
4+
*
5+
* In this specific problem structure, each node has a `next` pointer (to the
6+
* next node at the same level) and a `child` pointer (which points to the head
7+
* of another sorted linked list). The goal is to merge all these lists into a
8+
* single, vertically sorted linked list using the `child` pointer.
9+
*
10+
* The approach is a recursive one that leverages a merge utility, similar to
11+
* the merge step in Merge Sort. It recursively flattens the list starting from
12+
* the rightmost node and merges each node's child list with the already
13+
* flattened list to its right.
14+
* @see <a href="https://www.geeksforgeeks.org/flattening-a-linked-list/">GeeksforGeeks: Flattening a Linked List</a>
15+
*/
16+
public final class FlattenMultilevelLinkedList {
17+
/**
18+
* Private constructor to prevent instantiation of this utility class.
19+
*/
20+
private FlattenMultilevelLinkedList() {
21+
}
22+
/**
23+
* Node represents an element in the multilevel linked list. It contains the
24+
* integer data, a reference to the next node at the same level, and a
25+
* reference to the head of a child list.
26+
*/
27+
static class Node {
28+
int data;
29+
Node next;
30+
Node child;
31+
32+
Node(int data) {
33+
this.data = data;
34+
this.next = null;
35+
this.child = null;
36+
}
37+
}
38+
39+
/**
40+
* Merges two sorted linked lists (connected via the `child` pointer).
41+
* This is a helper function for the main flatten algorithm.
42+
*
43+
* @param a The head of the first sorted list.
44+
* @param b The head of the second sorted list.
45+
* @return The head of the merged sorted list.
46+
*/
47+
private static Node merge(Node a, Node b) {
48+
// If one of the lists is empty, return the other.
49+
if (a == null) {
50+
return b;
51+
}
52+
if (b == null) {
53+
return a;
54+
}
55+
56+
Node result;
57+
58+
// Choose the smaller value as the new head.
59+
if (a.data < b.data) {
60+
result = a;
61+
result.child = merge(a.child, b);
62+
} else {
63+
result = b;
64+
result.child = merge(a, b.child);
65+
}
66+
result.next = null; // Ensure the merged list has no `next` pointers.
67+
return result;
68+
}
69+
70+
/**
71+
* Flattens a multilevel linked list into a single sorted list.
72+
* The flattened list is connected using the `child` pointers.
73+
*
74+
* @param head The head of the top-level list (connected via `next` pointers).
75+
* @return The head of the fully flattened and sorted list.
76+
*/
77+
public static Node flatten(Node head) {
78+
// Base case: if the list is empty or has only one node, it's already flattened.
79+
if (head == null || head.next == null) {
80+
return head;
81+
}
82+
83+
// Recursively flatten the list starting from the next node.
84+
head.next = flatten(head.next);
85+
86+
// Now, merge the current list (head's child list) with the flattened rest of the list.
87+
head = merge(head, head.next);
88+
89+
// Return the head of the fully merged list.
90+
return head;
91+
}
92+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Test;
10+
/**
11+
* Unit tests for the FlattenMultilevelLinkedList class.
12+
* This class tests the flattening logic with various list structures,
13+
* including null lists, simple lists, and complex multilevel lists.
14+
*/
15+
final class FlattenMultilevelLinkedListTest {
16+
17+
// A helper function to convert a flattened list (connected by child pointers)
18+
// into a standard Java List for easy comparison.
19+
private List<Integer> toList(FlattenMultilevelLinkedList.Node head) {
20+
List<Integer> list = new ArrayList<>();
21+
FlattenMultilevelLinkedList.Node current = head;
22+
while (current != null) {
23+
list.add(current.data);
24+
current = current.child;
25+
}
26+
return list;
27+
}
28+
29+
@Test
30+
@DisplayName("Test with a null list")
31+
void testFlattenNullList() {
32+
assertNull(FlattenMultilevelLinkedList.flatten(null));
33+
}
34+
35+
@Test
36+
@DisplayName("Test with a simple, single-level list")
37+
void testFlattenSingleLevelList() {
38+
// Create a simple list: 1 -> 2 -> 3
39+
FlattenMultilevelLinkedList.Node head = new FlattenMultilevelLinkedList.Node(1);
40+
head.next = new FlattenMultilevelLinkedList.Node(2);
41+
head.next.next = new FlattenMultilevelLinkedList.Node(3);
42+
43+
// Flatten the list
44+
FlattenMultilevelLinkedList.Node flattenedHead = FlattenMultilevelLinkedList.flatten(head);
45+
46+
// Expected output: 1 -> 2 -> 3 (vertically)
47+
List<Integer> expected = List.of(1, 2, 3);
48+
assertEquals(expected, toList(flattenedHead));
49+
}
50+
51+
@Test
52+
@DisplayName("Test with a complex multilevel list")
53+
void testFlattenComplexMultilevelList() {
54+
// Create the multilevel structure from the problem description
55+
// 5 -> 10 -> 19 -> 28
56+
// | | | |
57+
// 7 20 22 35
58+
// | | |
59+
// 8 50 40
60+
// | |
61+
// 30 45
62+
FlattenMultilevelLinkedList.Node head = new FlattenMultilevelLinkedList.Node(5);
63+
head.child = new FlattenMultilevelLinkedList.Node(7);
64+
head.child.child = new FlattenMultilevelLinkedList.Node(8);
65+
head.child.child.child = new FlattenMultilevelLinkedList.Node(30);
66+
67+
head.next = new FlattenMultilevelLinkedList.Node(10);
68+
head.next.child = new FlattenMultilevelLinkedList.Node(20);
69+
70+
head.next.next = new FlattenMultilevelLinkedList.Node(19);
71+
head.next.next.child = new FlattenMultilevelLinkedList.Node(22);
72+
head.next.next.child.child = new FlattenMultilevelLinkedList.Node(50);
73+
74+
head.next.next.next = new FlattenMultilevelLinkedList.Node(28);
75+
head.next.next.next.child = new FlattenMultilevelLinkedList.Node(35);
76+
head.next.next.next.child.child = new FlattenMultilevelLinkedList.Node(40);
77+
head.next.next.next.child.child.child = new FlattenMultilevelLinkedList.Node(45);
78+
79+
// Flatten the list
80+
FlattenMultilevelLinkedList.Node flattenedHead = FlattenMultilevelLinkedList.flatten(head);
81+
82+
// Expected sorted output
83+
List<Integer> expected = List.of(5, 7, 8, 10, 19, 20, 22, 28, 30, 35, 40, 45, 50);
84+
assertEquals(expected, toList(flattenedHead));
85+
}
86+
87+
@Test
88+
@DisplayName("Test with some empty child lists")
89+
void testFlattenWithEmptyChildLists() {
90+
// Create a list: 5 -> 10 -> 12
91+
// | |
92+
// 7 11
93+
// |
94+
// 9
95+
FlattenMultilevelLinkedList.Node head = new FlattenMultilevelLinkedList.Node(5);
96+
head.child = new FlattenMultilevelLinkedList.Node(7);
97+
head.child.child = new FlattenMultilevelLinkedList.Node(9);
98+
99+
head.next = new FlattenMultilevelLinkedList.Node(10); // No child list
100+
head.next.child = null;
101+
102+
head.next.next = new FlattenMultilevelLinkedList.Node(12);
103+
head.next.next.child = new FlattenMultilevelLinkedList.Node(16);
104+
105+
// Flatten the list
106+
FlattenMultilevelLinkedList.Node flattenedHead = FlattenMultilevelLinkedList.flatten(head);
107+
108+
// Expected sorted output
109+
List<Integer> expected = List.of(5, 7, 9, 10, 12, 16);
110+
assertEquals(expected, toList(flattenedHead));
111+
}
112+
}

0 commit comments

Comments
 (0)