📖 Scenario: You are working on a contact app that keeps two separate sorted lists of contacts by their ID numbers. You want to combine these two lists into one sorted list so you can show all contacts in order.
🎯 Goal: Build a program that merges two sorted linked lists into one sorted linked list.
📋 What You'll Learn
Create two sorted linked lists with exact values
Create a helper variable to track the start of the merged list
Write the logic to merge the two sorted linked lists
Print the merged linked list in order
💡 Why This Matters
🌍 Real World
Merging sorted lists is common in apps that combine data from multiple sources, like contacts, messages, or events.
💼 Career
Understanding linked lists and merging algorithms is important for software engineering roles involving data structures and efficient data processing.
Progress0 / 4 steps
1
Create two sorted linked lists
Create a linked list called list1 with nodes containing values 1, 3, and 5. Create another linked list called list2 with nodes containing values 2, 4, and 6. Use the provided ListNode class.
DSA C#
Hint
Use the ListNode constructor to link nodes by passing the next node as the second argument.
2
Create a dummy head for the merged list
Create a variable called dummy as a new ListNode with value 0. Create a variable called current and set it equal to dummy. This will help track the start and current position of the merged list.
DSA C#
Hint
The dummy node helps to easily return the head of the merged list later.
3
Merge the two sorted linked lists
Use a while loop that runs while both list1 and list2 are not null. Inside the loop, compare list1.val and list2.val. Attach the smaller node to current.next and move that list forward. Move current forward as well. After the loop, attach the remaining nodes from list1 or list2 to current.next.
DSA C#
Hint
Use a loop to pick the smaller node from the two lists and attach it to the merged list. After the loop, attach the leftover nodes.
4
Print the merged linked list
Create a variable called node and set it to dummy.next. Use a while loop to traverse the merged list. Inside the loop, print the value of node.val followed by " -> ". Move node to node.next. After the loop, print "null" to mark the end.
DSA C#
Hint
Traverse from dummy.next and print each node's value followed by " -> ". End with "null".
Practice
(1/5)
1. What is the main goal when merging two sorted linked lists?
easy
A. Remove duplicates from both lists
B. Create a new sorted linked list containing all elements from both lists
C. Reverse both linked lists
D. Sort each list individually without merging
Solution
Step 1: Understand the problem goal
The goal is to combine two sorted linked lists into one sorted linked list.
Step 2: Clarify the expected output
The output should be a single linked list that maintains sorted order and contains all nodes from both input lists.
Final Answer:
Create a new sorted linked list containing all elements from both lists -> Option B
Quick Check:
Merge means combine sorted lists into one sorted list [OK]
Hint: Merging means combining sorted lists into one sorted list [OK]
Common Mistakes:
Thinking merge means reversing lists
Trying to sort lists separately without merging
Removing duplicates instead of merging
2. Which of the following is the correct C# syntax to declare a node class for a singly linked list used in merging?
easy
A. class Node { int val; Node next; Node(int x) { val = x; next = null; } }
B. public struct Node { int val; Node next; }
C. public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } }
D. public class Node { int val; int next; public Node(int x) { val = x; next = 0; } }
Solution
Step 1: Check class declaration and access modifiers
public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } } correctly declares a public class with public fields and a constructor initializing values.
Step 2: Verify field types and initialization
public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } } uses 'int val' and 'Node next' with proper initialization; others miss public or use wrong types.
Final Answer:
public class Node { public int val; public Node next; public Node(int x) { val = x; next = null; } } -> Option C
Quick Check:
Correct class with public fields and constructor [OK]
Hint: Node class needs public fields and constructor for linked list [OK]
Common Mistakes:
Missing public keyword causing access errors
Using struct instead of class for linked list node
Incorrect type for next pointer
3. What will be the output linked list after merging these two sorted lists?