Start with heads: 1 (List1) and 2 (List2). 1 < 2, so 1 goes first. Then compare 3 and 2, 2 < 3, so 2 goes next. Continue this way to merge in sorted order.
Step 2: Build merged list step-by-step
Merged list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL.
Final Answer:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL -> Option C
Quick Check:
Merge keeps sorted order [OK]
Hint: Pick smaller node each step to merge sorted lists [OK]
Common Mistakes:
Appending one list after the other without sorting
Reversing the order accidentally
Skipping nodes during merge
4. Consider this buggy merge function snippet in C:
What is the main issue if this function is called with two sorted lists?
medium
A. It reverses the merged list
B. It merges lists correctly without issues
C. It loses nodes when one list is empty
D. It causes infinite recursion when lists have equal elements
Solution
Step 1: Analyze base cases
If one list is empty (!l1 or !l2), it correctly returns the other list.
Step 2: Analyze recursive case
If l1->data < l2->data, it sets l1->next to the merge of the rest and returns l1. Otherwise, sets l2->next to merge(l1, l2->next) and returns l2. However, when l1->data == l2->data, the else branch is taken repeatedly without advancing l1, causing infinite recursion.
Final Answer:
It causes infinite recursion when lists have equal elements -> Option D
Quick Check:
Equal elements cause infinite recursion due to no advancement in l1 [OK]
Hint: Recursive merge must handle equal elements to avoid infinite recursion [OK]
Common Mistakes:
Thinking equal elements cause infinite recursion
Believing it loses nodes on empty lists
Assuming it reverses the list
5. You have two sorted linked lists: List1: 1 -> 4 -> 7 -> NULL List2: 2 -> 4 -> 6 -> 8 -> NULL
Write the merged sorted linked list after merging them and removing duplicate values.
hard
A. 1 -> 4 -> 7 -> 2 -> 4 -> 6 -> 8 -> NULL
B. 1 -> 2 -> 4 -> 6 -> 7 -> 8 -> NULL
C. 1 -> 2 -> 4 -> 4 -> 6 -> 7 -> 8 -> NULL
D. 1 -> 2 -> 6 -> 7 -> 8 -> NULL
Solution
Step 1: Merge lists in sorted order
Merge elements: 1, 2, 4, 4, 6, 7, 8.
Step 2: Remove duplicate values
Remove one of the duplicate 4's to get: 1 -> 2 -> 4 -> 6 -> 7 -> 8 -> NULL.
Final Answer:
1 -> 2 -> 4 -> 6 -> 7 -> 8 -> NULL -> Option B
Quick Check:
Merge + remove duplicates = sorted unique list [OK]
Hint: Merge then skip duplicates for unique sorted list [OK]
Common Mistakes:
Leaving duplicates in merged list
Appending lists without sorting
Removing unique elements by mistake
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.