📖 Scenario: You are working on a contact app that keeps two separate lists of contacts sorted by their ID numbers. You want to combine these two sorted lists into one sorted list so that you can show all contacts in order.
🎯 Goal: Build a Java program that merges two sorted linked lists of integers into one sorted linked list.
📋 What You'll Learn
Create two sorted linked lists using the ListNode class
Create a helper variable to start the merged list
Write the core logic to merge the two sorted lists into one sorted list
Print the merged linked list in the format: value -> value -> ... -> null
💡 Why This Matters
🌍 Real World
Merging sorted lists is common in contact apps, calendars, and any place where sorted data from different sources needs to be combined.
💼 Career
Understanding linked list merging is important for coding interviews and working with data structures in software development.
Progress0 / 4 steps
1
Create two sorted linked lists
Create a class called ListNode with an integer val and a next pointer. Then create two sorted linked lists called list1 and list2 with these exact values: list1: 1 -> 3 -> 5 -> null list2: 2 -> 4 -> 6 -> null
DSA Java
Hint
Start by defining the ListNode class with a constructor. Then create the two lists by linking nodes with the given values.
2
Create a dummy node to start the merged list
Inside the main method, create a ListNode variable called dummy with value 0. Also create a ListNode variable called current and set it equal to dummy. This will help build the merged list.
DSA Java
Hint
The dummy node helps to easily return the head of the merged list later. The current pointer will move as you add nodes.
3
Merge the two sorted linked lists
Use a while loop to compare the val of list1 and list2. Attach the smaller node to current.next and move that list's pointer forward. Also move current forward. After the loop, attach the remaining nodes from list1 or list2 to current.next.
DSA Java
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 ListNode variable called merged and set it to dummy.next. Use a while loop to print each node's val followed by " -> ". After the loop, print "null" to show the end of the list.
DSA Java
Hint
Start from dummy.next and print each node's value followed by " -> ". End with printing "null".
Practice
(1/5)
1. What is the main goal when merging two sorted linked lists?
easy
A. To create a new sorted linked list containing all elements from both lists
B. To reverse both linked lists
C. To find the maximum element in both lists
D. To delete duplicate elements from both lists
Solution
Step 1: Understand the input lists
Two linked lists are sorted individually, meaning their elements are in ascending order.
Step 2: Merge while maintaining order
The goal is to combine these two lists into one sorted list that contains all elements from both.
Final Answer:
To create a new sorted linked list containing all elements from both lists -> Option A
Quick Check:
Merging sorted lists means combining all elements in order [OK]
Hint: Merging means combining sorted lists into one sorted list [OK]
Common Mistakes:
Thinking merge means reversing lists
Assuming merge removes duplicates
Confusing merge with finding max element
2. Which of the following Java method signatures correctly represents a function to merge two sorted linked lists of integers?
easy
A. public int merge(ListNode l1, ListNode l2)
B. public void merge(int l1, int l2)
C. public ListNode merge(ListNode l1, ListNode l2)
D. public ListNode merge(int[] l1, int[] l2)
Solution
Step 1: Identify correct parameter types
The function should accept two linked lists, so parameters must be ListNode types, not primitives or arrays.
Step 2: Identify correct return type
The function returns a merged linked list, so return type should be ListNode, not void or int.
Final Answer:
public ListNode merge(ListNode l1, ListNode l2) -> Option C
Quick Check:
Method with ListNode params and return type is correct [OK]
Hint: Method merges two ListNode inputs and returns ListNode [OK]
Common Mistakes:
Using primitive types instead of ListNode
Returning void instead of ListNode
Using arrays instead of linked lists
3. What is the output linked list after merging these two sorted lists?
List 1: 1 -> 3 -> 5 -> null
List 2: 2 -> 4 -> 6 -> null
medium
A. 2 -> 4 -> 6 -> 1 -> 3 -> 5 -> null
B. 1 -> 3 -> 5 -> 2 -> 4 -> 6 -> null
C. 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> null
D. 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null
Solution
Step 1: Compare first nodes of both lists
1 (List 1) is less than 2 (List 2), so 1 is first in merged list.
Step 2: Continue comparing next nodes
Next compare 3 and 2, 2 is smaller, add 2, then 3 vs 4, add 3, then 5 vs 4, add 4, then 5 vs 6, add 5, finally add 6.
Final Answer:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> null -> Option D
Quick Check:
Merge keeps ascending order combining both lists [OK]
Hint: Pick smaller head node repeatedly to merge sorted lists [OK]
Common Mistakes:
Appending one list after the other without sorting
Reversing order of merged list
Skipping nodes during merge
4. Given the following code snippet to merge two sorted linked lists, what is the error?