What if you could combine two sorted lists perfectly without lifting a pen or losing track?
Why Merge Two Sorted Linked Lists in DSA C?
Imagine you have two lists of names sorted alphabetically on paper. You want to combine them into one big sorted list by comparing each name manually and writing them down one by one.
Doing this by hand is slow and easy to mess up. You might lose track of which name comes next or accidentally skip some names. It takes a lot of time and effort to keep the order correct.
Using a program to merge two sorted linked lists automatically compares the first items of each list and picks the smaller one step-by-step. It keeps the order perfectly without missing anything and finishes quickly.
while(list1 not empty and list2 not empty) { if(list1 head < list2 head) { add list1 head to result move list1 head forward } else { add list2 head to result move list2 head forward } }
struct Node* mergeSortedLists(struct Node* list1, struct Node* list2) {
if(!list1) return list2;
if(!list2) return list1;
if(list1->data < list2->data) {
list1->next = mergeSortedLists(list1->next, list2);
return list1;
} else {
list2->next = mergeSortedLists(list1, list2->next);
return list2;
}
}This lets you combine sorted data quickly and correctly, making it easy to handle bigger lists or streams of sorted information.
Think about merging two sorted contact lists from different phones into one combined phonebook without losing the alphabetical order.
Manual merging is slow and error-prone.
Automated merging compares and links nodes step-by-step.
Result is a perfectly sorted combined linked list.
Practice
Solution
Step 1: Understand the merge operation
Merging two sorted linked lists means combining their elements into one list that remains sorted.Step 2: Identify the goal
The goal is to create a new sorted linked list that contains all elements from both input lists in order.Final Answer:
To create a new sorted linked list containing all elements from both lists -> Option AQuick Check:
Merge = combine sorted lists [OK]
- Thinking merge reverses lists
- Confusing merge with finding max element
- Assuming merge removes duplicates
Solution
Step 1: Check struct syntax for linked list node
In C, a node contains data and a pointer to the next node. The pointer must be declared as 'struct Node* next;'.Step 2: Identify correct declaration
struct Node { int data; struct Node* next; }; correctly declares 'struct Node' with 'int data' and 'struct Node* next;'. struct Node { int data; Node* next; }; misses 'struct' keyword before Node*, struct Node { int data; struct Node next; }; incorrectly uses 'struct Node next;' (not a pointer), and struct Node { int data; struct Node* prev; }; uses 'prev' which is for doubly linked lists.Final Answer:
struct Node { int data; struct Node* next; }; -> Option AQuick Check:
Node struct needs pointer to next node [OK]
- Omitting 'struct' keyword before Node*
- Using node instead of pointer for next
- Confusing singly linked list with doubly linked list
List1: 1 -> 3 -> 5 -> NULL
List2: 2 -> 4 -> 6 -> NULL
Assuming the merge function merges correctly.
Solution
Step 1: Compare elements from both lists in order
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 CQuick Check:
Merge keeps sorted order [OK]
- Appending one list after the other without sorting
- Reversing the order accidentally
- Skipping nodes during merge
struct Node* merge(struct Node* l1, struct Node* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->data < l2->data) {
l1->next = merge(l1->next, l2);
return l1;
} else {
l2->next = merge(l1, l2->next);
return l2;
}
}What is the main issue if this function is called with two sorted lists?
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 DQuick Check:
Equal elements cause infinite recursion due to no advancement in l1 [OK]
- Thinking equal elements cause infinite recursion
- Believing it loses nodes on empty lists
- Assuming it reverses the list
List1: 1 -> 4 -> 7 -> NULL
List2: 2 -> 4 -> 6 -> 8 -> NULL
Write the merged sorted linked list after merging them and removing duplicate values.
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 BQuick Check:
Merge + remove duplicates = sorted unique list [OK]
- Leaving duplicates in merged list
- Appending lists without sorting
- Removing unique elements by mistake
Ready to prove it?
Pass the test and this pattern moves unknown
