Challenge - 5 Problems
Master of Merging Sorted 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 Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class MergeLists { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode 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; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { ListNode l1 = new ListNode(1); l1.next = new ListNode(3); l1.next.next = new ListNode(5); ListNode l2 = new ListNode(2); l2.next = new ListNode(4); l2.next.next = new ListNode(6); ListNode merged = mergeTwoLists(l1, l2); printList(merged); } }
Attempts:
2 left
💡 Hint
Compare the values of nodes from both lists one by one and link the smaller one first.
✗ Incorrect
The merge function compares nodes from both lists and appends the smaller node to the merged list. This results in a sorted merged list combining all nodes.
❓ Predict Output
intermediateOutput when one list is empty
What is the printed state of the merged linked list after merging these two lists?
List 1: null
List 2: 0 -> 7 -> 8 -> null
List 1: null
List 2: 0 -> 7 -> 8 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class MergeLists { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode 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; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { ListNode l1 = null; ListNode l2 = new ListNode(0); l2.next = new ListNode(7); l2.next.next = new ListNode(8); ListNode merged = mergeTwoLists(l1, l2); 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 just the second list unchanged.
🔧 Debug
advancedIdentify the bug in the merge function
What error will this code produce when merging two sorted linked lists?
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class MergeLists { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode 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; } // Bug: forgot to attach remaining nodes return dummy.next; } }
Attempts:
2 left
💡 Hint
Check what happens after the while loop ends.
✗ Incorrect
The code does not attach the remaining nodes from either list after the while loop, so the merged list is incomplete.
❓ Predict Output
advancedOutput of 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 -> null
List 2: 1 -> 2 -> 5 -> null
List 1: 1 -> 3 -> 5 -> null
List 2: 1 -> 2 -> 5 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class MergeLists { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode 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; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { ListNode l1 = new ListNode(1); l1.next = new ListNode(3); l1.next.next = new ListNode(5); ListNode l2 = new ListNode(1); l2.next = new ListNode(2); l2.next.next = new ListNode(5); ListNode merged = mergeTwoLists(l1, l2); printList(merged); } }
Attempts:
2 left
💡 Hint
Duplicates are allowed and should appear in order.
✗ Incorrect
The merge function includes duplicates by comparing with <= and appending nodes accordingly.
❓ Predict Output
expertOutput after merging lists with negative and positive values
What is the printed state of the merged linked list after merging these two sorted lists?
List 1: -3 -> 0 -> 4 -> null
List 2: -2 -> 1 -> 3 -> null
List 1: -3 -> 0 -> 4 -> null
List 2: -2 -> 1 -> 3 -> null
DSA Java
class ListNode { int val; ListNode next; ListNode(int val) { this.val = val; } } public class MergeLists { public static ListNode mergeTwoLists(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode 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; } public static void printList(ListNode head) { while (head != null) { System.out.print(head.val + " -> "); head = head.next; } System.out.print("null"); } public static void main(String[] args) { ListNode l1 = new ListNode(-3); l1.next = new ListNode(0); l1.next.next = new ListNode(4); ListNode l2 = new ListNode(-2); l2.next = new ListNode(1); l2.next.next = new ListNode(3); ListNode merged = mergeTwoLists(l1, l2); printList(merged); } }
Attempts:
2 left
💡 Hint
Negative values are merged in sorted order just like positive values.
✗ Incorrect
The merge function compares values and merges nodes in ascending order regardless of sign.
