📖 Scenario: You are working on a contact list app. You have two separate lists of contacts sorted by their ID numbers. You want to combine these two lists into one sorted list so that users can see all contacts in order.
🎯 Goal: Build a program that merges two sorted singly linked lists into one sorted linked list.
📋 What You'll Learn
Create two sorted singly linked lists with given values
Use a pointer to traverse and merge the lists
Create a new sorted linked list by merging the two lists
Print the merged linked list in order
💡 Why This Matters
🌍 Real World
Merging sorted lists is common in apps like contact managers, calendars, or any system that combines sorted data streams.
💼 Career
Understanding linked list merging is important for software engineers working on data processing, system design, and algorithm optimization.
Progress0 / 4 steps
1
Create Two Sorted Linked Lists
Create a singly linked list node struct called ListNode with an int val and a ListNode* next. Then create two sorted linked lists called list1 and list2 with these exact values: list1 contains 1 -> 3 -> 5 -> null, and list2 contains 2 -> 4 -> 6 -> null.
DSA C++
Hint
Define the node structure first. Then link nodes by setting the next pointer to the next node.
2
Create a Dummy Node and a Pointer for Merging
Create a dummy ListNode called dummy with value 0. Create a pointer current and set it to point to dummy. This will help build the merged list.
DSA C++
Hint
The dummy node helps simplify edge cases. The current pointer will move along the new list as you add nodes.
3
Merge the Two Sorted Lists
Use a while loop to compare list1->val and list2->val. Attach the smaller node to current->next and move that list's pointer forward. Move current forward as well. After the loop, attach the remaining nodes from list1 or list2 to current->next.
DSA C++
Hint
Keep moving the current pointer and attach the smaller node each time. After the loop, attach the leftover nodes.
4
Print the Merged Linked List
Use a ListNode* pointer called temp to traverse the merged list starting from dummy->next. Print each node's val followed by -> . After the last node, print null.
DSA C++
Hint
Start from dummy->next because dummy is a placeholder. Print each value followed by -> . End with null.
Practice
(1/5)
1. What is the main goal when merging two sorted linked lists into one sorted linked list?
easy
A. Sort each list separately before merging
B. Reverse the order of nodes in both lists
C. Combine nodes from both lists maintaining sorted order
D. Remove duplicate nodes from both lists
Solution
Step 1: Understand the merge operation
Merging means combining two lists into one while keeping the order sorted.
Step 2: Maintain sorted order during merge
We pick the smaller node from the heads of both lists repeatedly to keep the merged list sorted.
Final Answer:
Combine nodes from both lists maintaining sorted order -> Option C
Quick Check:
Merge = Combine sorted lists [OK]
Hint: Always pick the smaller node first to keep order [OK]
Common Mistakes:
Trying to reverse lists instead of merging
Sorting lists again after merging
Ignoring order and just concatenating
2. Which of the following is the correct way to declare a node structure for a singly linked list in C++?
easy
A. struct Node { int data; Node next; };
B. class Node { int data; Node next; };
C. class Node { int data; Node* next; };
D. struct Node { int data; Node* next; };
Solution
Step 1: Understand node structure in C++
A node should have data and a pointer to the next node.
Step 2: Check pointer usage
Using Node* next is correct to point to the next node; Node next would cause infinite size error.
Final Answer:
struct Node { int data; Node* next; }; -> Option D
Quick Check:
Node pointer = Node* next [OK]
Hint: Use pointers for next node in linked list [OK]
Common Mistakes:
Using object instead of pointer for next node
Missing semicolon after struct
Using class without access specifier
3. What will be the output linked list after merging these two sorted lists? List1: 1 -> 3 -> 5 -> null List2: 2 -> 4 -> 6 -> null Using a standard merge algorithm?
medium
A. 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
B. 1 -> 3 -> 5 -> 2 -> 4 -> 6 -> null
C. 2 -> 4 -> 6 -> 1 -> 3 -> 5 -> null
D. 1 -> 2 -> 4 -> 3 -> 5 -> 6 -> null
Solution
Step 1: Compare heads of both lists
Start with 1 (List1) and 2 (List2). 1 is smaller, so first node is 1.
Step 2: Continue picking smaller nodes
Next compare 3 and 2, pick 2; then 3 and 4, pick 3; then 5 and 4, pick 4; then 5 and 6, pick 5; finally 6.
Final Answer:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null -> Option A
Quick Check:
Sorted merge = 1,2,3,4,5,6 [OK]
Hint: Pick smaller head node repeatedly [OK]
Common Mistakes:
Appending one list after the other without sorting
Mixing node order incorrectly
Skipping nodes during merge
4. Given the following code snippet to merge two sorted linked lists, what is the error?
A. No error, code correctly merges two sorted lists
B. Missing base case for empty lists
C. Incorrect comparison operator used
D. Function does not handle cyclic lists
Solution
Step 1: Check base cases
Code returns the other list if one is empty, which is correct.
Step 2: Check recursive merge logic
It compares data correctly and recursively merges the rest, returning the smaller node.
Final Answer:
No error, code correctly merges two sorted lists -> Option A
Quick Check:
Recursive merge logic correct [OK]
Hint: Check base cases and recursion carefully [OK]
Common Mistakes:
Forgetting to return merged list
Wrong base case conditions
Using wrong pointers in recursion
5. You have two sorted linked lists: List1: 1 -> 4 -> 5 -> 7 -> null List2: 2 -> 3 -> 6 -> 8 -> 9 -> null Write the merged list after merging them. Then, if you want to remove duplicates from the merged list, what will be the final list? Assume duplicates can appear after merge.