Challenge - 5 Problems
Merge Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after merging two sorted linked lists?
Given two sorted linked lists:
List1: 1 -> 3 -> 5 -> null
List2: 2 -> 4 -> 6 -> null
What is the merged linked list?
List1: 1 -> 3 -> 5 -> null
List2: 2 -> 4 -> 6 -> null
What is the merged linked list?
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function mergeTwoLists(l1, l2) {
let dummy = new ListNode(0);
let current = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val < l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = l1 !== null ? l1 : l2;
return dummy.next;
}
// Construct lists
let l1 = new ListNode(1, new ListNode(3, new ListNode(5)));
let l2 = new ListNode(2, new ListNode(4, new ListNode(6)));
let merged = mergeTwoLists(l1, l2);
// Function to print list
function printList(head) {
let result = [];
while (head !== null) {
result.push(head.val);
head = head.next;
}
return result.join(' -> ') + ' -> null';
}
console.log(printList(merged));Attempts:
2 left
💡 Hint
Compare nodes one by one and attach the smaller node to the merged list.
✗ Incorrect
The function compares the current nodes of both lists and attaches the smaller one to the merged list, continuing until all nodes are merged in sorted order.
❓ Predict Output
intermediateWhat is the output of merging when one list is empty?
Given two linked lists:
List1: null
List2: 0 -> 1 -> 2 -> null
What is the merged linked list?
List1: null
List2: 0 -> 1 -> 2 -> null
What is the merged linked list?
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function mergeTwoLists(l1, l2) {
let dummy = new ListNode(0);
let current = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val < l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = l1 !== null ? l1 : l2;
return dummy.next;
}
// Construct lists
let l1 = null;
let l2 = new ListNode(0, new ListNode(1, new ListNode(2)));
let merged = mergeTwoLists(l1, l2);
// Function to print list
function printList(head) {
let result = [];
while (head !== null) {
result.push(head.val);
head = head.next;
}
return result.join(' -> ') + ' -> null';
}
console.log(printList(merged));Attempts:
2 left
💡 Hint
If one list is empty, the merged list is just the other list.
✗ Incorrect
Since the first list is empty, the merged list is simply the second list unchanged.
🔧 Debug
advancedWhy does this merge function cause an infinite loop?
Consider this merge function for two sorted linked lists. It causes an infinite loop. Why?
function mergeTwoLists(l1, l2) { let dummy = new ListNode(0); let current = dummy; while (l1 !== null && l2 !== null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } // Missing current = current.next here } current.next = l1 !== null ? l1 : l2; return dummy.next; }
function mergeTwoLists(l1, l2) { let dummy = new ListNode(0); let current = dummy; while (l1 !== null && l2 !== null) { if (l1.val < l2.val) { current.next = l1; l1 = l1.next; } else { current.next = l2; l2 = l2.next; } // Missing current = current.next here } current.next = l1 !== null ? l1 : l2; return dummy.next; }
Attempts:
2 left
💡 Hint
Check if the 'current' pointer moves forward inside the loop.
✗ Incorrect
The 'current' pointer must move forward after attaching a node. Without 'current = current.next', the same node is attached repeatedly causing infinite loop.
🧠 Conceptual
advancedWhat is the time complexity of merging two sorted linked lists of lengths m and n?
You have two sorted linked lists with lengths m and n. What is the time complexity of merging them into one sorted linked list?
Attempts:
2 left
💡 Hint
Each node is visited once during merging.
✗ Incorrect
The merge process visits each node exactly once, so the time complexity is linear in the total number of nodes.
❓ Predict Output
expertWhat is the output after merging two sorted linked lists with duplicate values?
Given two sorted linked lists:
List1: 1 -> 3 -> 5 -> 7 -> null
List2: 1 -> 2 -> 5 -> 6 -> null
What is the merged linked list?
List1: 1 -> 3 -> 5 -> 7 -> null
List2: 1 -> 2 -> 5 -> 6 -> null
What is the merged linked list?
DSA Javascript
function ListNode(val, next = null) {
this.val = val;
this.next = next;
}
function mergeTwoLists(l1, l2) {
let dummy = new ListNode(0);
let current = dummy;
while (l1 !== null && l2 !== null) {
if (l1.val <= l2.val) {
current.next = l1;
l1 = l1.next;
} else {
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
current.next = l1 !== null ? l1 : l2;
return dummy.next;
}
// Construct lists
let l1 = new ListNode(1, new ListNode(3, new ListNode(5, new ListNode(7))));
let l2 = new ListNode(1, new ListNode(2, new ListNode(5, new ListNode(6))));
let merged = mergeTwoLists(l1, l2);
// Function to print list
function printList(head) {
let result = [];
while (head !== null) {
result.push(head.val);
head = head.next;
}
return result.join(' -> ') + ' -> null';
}
console.log(printList(merged));Attempts:
2 left
💡 Hint
Duplicates are allowed and merged in sorted order.
✗ Incorrect
The merge function includes duplicates and maintains sorted order by choosing the smaller or equal node first.
