-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflattening-a-linked-list.cpp
42 lines (34 loc) · 1.06 KB
/
flattening-a-linked-list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Function to merge two sorted linked lists
Node* mergeTwoLists(Node* list1, Node* list2) {
if (!list1) return list2;
if (!list2) return list1;
Node* result;
if (list1->data < list2->data) {
result = list1;
result->child = mergeTwoLists(list1->child, list2);
} else {
result = list2;
result->child = mergeTwoLists(list1, list2->child);
}
result->next = nullptr; // Ensure next is null in the merged list
return result;
}
// Function to flatten the multilevel linked list into a single list sorted order
Node* flattenLinkedList(Node* head) {
if (!head) {
return nullptr;
}
// Recursively flatten the next list
Node* flattenedNext = flattenLinkedList(head->next);
// Merge the current node's child list with the flattened next list
head = mergeTwoLists(head, flattenedNext);
return head;
}
// Utility function to print the flattened list
void printList(Node* head) {
while (head) {
cout << head->data << " ";
head = head->child;
}
cout << endl;
}