Challenge - 5 Problems
Master of Merging Linked Lists
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of merging two sorted linked lists
What is the printed state of the merged linked list after merging these two sorted lists?
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
DSA C++
struct Node {
int val;
Node* next;
Node(int x) : val(x), next(nullptr) {}
};
Node* mergeLists(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = mergeLists(l1->next, l2);
return l1;
} else {
l2->next = mergeLists(l1, l2->next);
return l2;
}
}
// After merging, print the list
void printList(Node* head) {
while (head) {
std::cout << head->val << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
// Assume lists are created and merged as aboveAttempts:
2 left
💡 Hint
Think about how two sorted lists merge by always choosing the smaller current element.
✗ Incorrect
The merge function compares the heads of both lists and picks the smaller value first, then recursively merges the rest. This results in a fully sorted merged list.
❓ Predict Output
intermediateOutput of iterative merge of two sorted linked lists
What is the printed state of the merged linked list after merging these two sorted lists using an iterative approach?
List 1: 1 -> 4 -> 7 -> null
List 2: 2 -> 3 -> 6 -> null
List 1: 1 -> 4 -> 7 -> null
List 2: 2 -> 3 -> 6 -> null
DSA C++
struct Node {
int val;
Node* next;
Node(int x) : val(x), next(nullptr) {}
};
Node* mergeListsIterative(Node* l1, Node* l2) {
Node dummy(0);
Node* tail = &dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
tail->next = l1;
l1 = l1->next;
} else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = l1 ? l1 : l2;
return dummy.next;
}
// After merging, print the list
void printList(Node* head) {
while (head) {
std::cout << head->val << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
// Assume lists are created and merged as aboveAttempts:
2 left
💡 Hint
The iterative method also picks the smaller node each time, building the merged list step by step.
✗ Incorrect
The iterative merge uses a dummy node and a tail pointer to append the smaller node from either list until one list is empty, then appends the rest.
🔧 Debug
advancedIdentify the error in this merge function
What error will this merge function cause when merging two sorted linked lists?
DSA C++
Node* mergeLists(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val <= l2->val) {
l1->next = mergeLists(l1->next, l2);
return l1;
} else {
l2->next = mergeLists(l1, l2->next);
return l2;
}
return nullptr;
}Attempts:
2 left
💡 Hint
Check the base cases and recursive calls carefully.
✗ Incorrect
The function correctly handles base cases and merges recursively without infinite loops or null dereferences. No memory leak occurs as nodes are reused.
❓ Predict Output
advancedOutput after merging lists with duplicate values
What is the printed state of the merged linked list after merging these two sorted lists?
List 1: 1 -> 3 -> 5 -> 7 -> null
List 2: 3 -> 5 -> 8 -> null
List 1: 1 -> 3 -> 5 -> 7 -> null
List 2: 3 -> 5 -> 8 -> null
DSA C++
struct Node {
int val;
Node* next;
Node(int x) : val(x), next(nullptr) {}
};
Node* mergeLists(Node* l1, Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = mergeLists(l1->next, l2);
return l1;
} else {
l2->next = mergeLists(l1, l2->next);
return l2;
}
}
// After merging, print the list
void printList(Node* head) {
while (head) {
std::cout << head->val << " -> ";
head = head->next;
}
std::cout << "null" << std::endl;
}
// Assume lists are created and merged as aboveAttempts:
2 left
💡 Hint
Duplicates appear in order as they come from both lists.
✗ Incorrect
The merge function preserves order and includes duplicates from both lists in sorted order.
🧠 Conceptual
expertTime complexity of merging two sorted linked lists
What is the time complexity of merging two sorted linked lists of lengths m and n?
Attempts:
2 left
💡 Hint
Consider how many nodes are visited during the merge.
✗ Incorrect
Each node from both lists is visited exactly once during the merge, so the time is proportional to the sum of their lengths.
