What is the main idea behind merge sort when sorting a linked list?
Merge sort divides the linked list into two halves, recursively sorts each half, and then merges the two sorted halves back together.
Click to reveal answer
intermediate
Why is merge sort preferred over quicksort for sorting linked lists?
Merge sort works well with linked lists because it does not require random access and can merge two sorted lists efficiently by changing pointers, while quicksort relies on random access which linked lists do not support efficiently.
Click to reveal answer
beginner
How do you find the middle of a linked list efficiently?
Use two pointers: a slow pointer that moves one step at a time and a fast pointer that moves two steps at a time. When the fast pointer reaches the end, the slow pointer will be at the middle.
Click to reveal answer
beginner
What is the time complexity of merge sort on a linked list?
The time complexity is O(n log n), where n is the number of nodes in the linked list.
Click to reveal answer
intermediate
What is the space complexity of merge sort on a linked list?
The space complexity is O(log n) due to the recursion stack used during the divide steps.
Click to reveal answer
Which technique is used to split the linked list into two halves in merge sort?
ARandomly splitting the list
BTwo pointers: slow and fast
CUsing a stack to store nodes
DCounting nodes and dividing by two
✗ Incorrect
Two pointers, slow and fast, are used to find the middle efficiently without counting all nodes.
What is the main operation performed after recursively sorting the two halves in merge sort?
ASwapping nodes randomly
BReversing the list
CMerging the two sorted halves
DDeleting duplicate nodes
✗ Incorrect
After sorting each half, merge sort merges the two sorted halves into one sorted list.
Why is merge sort efficient for linked lists compared to quicksort?
ABecause merge sort uses pointer manipulation without random access
BBecause quicksort is not a sorting algorithm
CBecause merge sort uses extra arrays
DBecause quicksort requires recursion
✗ Incorrect
Merge sort efficiently merges by changing pointers, while quicksort needs random access which linked lists lack.
What is the base case for the recursive merge sort on a linked list?
AWhen the list is fully sorted
BWhen the list has two nodes
CWhen the list has more than ten nodes
DWhen the list is empty or has only one node
✗ Incorrect
A list with zero or one node is already sorted, so recursion stops there.
What is the output format after sorting a linked list using merge sort?
AA sorted linked list with nodes connected in ascending order
BAn array of sorted values
CA reversed linked list
DA list of unsorted nodes
✗ Incorrect
The output is a linked list sorted in ascending order, with nodes connected properly.
Explain step-by-step how merge sort sorts a linked list.
Think about dividing and conquering the list.
You got /4 concepts.
Describe how merging two sorted linked lists works in merge sort.
Imagine merging two sorted lines of people by height.
You got /4 concepts.
Practice
(1/5)
1. What is the main advantage of using merge sort to sort a linked list compared to quicksort?
easy
A. Quicksort is unstable, but merge sort is always unstable.
B. Merge sort uses less memory than quicksort for linked lists.
C. Merge sort does not require random access, which suits linked lists well.
D. Merge sort sorts linked lists in-place without extra space.
Solution
Step 1: Understand linked list access
Linked lists do not support fast random access, unlike arrays.
Step 2: Compare sorting algorithms
Merge sort works by splitting and merging, which only needs sequential access, making it suitable for linked lists.
Final Answer:
Merge sort does not require random access, which suits linked lists well. -> Option C
Quick Check:
Linked list + merge sort = no random access needed [OK]
Hint: Merge sort fits linked lists because it uses sequential access [OK]
Common Mistakes:
Thinking quicksort is better for linked lists
Believing merge sort sorts in-place without extra space
Confusing stability properties of sorting algorithms
2. Which of the following is the correct base case condition in a recursive merge sort function for a linked list?
easy
A. if head is None and head.next is None: return None
B. if head is None or head.next is None: return None
C. if head.next is None: return None
D. if head is None or head.next is None: return head
Solution
Step 1: Identify base case for recursion
When the list is empty or has one node, it is already sorted.
Step 2: Correct base case condition
Check if head is None (empty list) or head.next is None (single node), then return head.
Final Answer:
if head is None or head.next is None: return head -> Option D
Quick Check:
Base case returns head when list size ≤ 1 [OK]
Hint: Base case returns head if list empty or single node [OK]
Common Mistakes:
Returning None instead of head for single node
Using 'and' instead of 'or' in condition
Missing base case leading to infinite recursion
3. What will be the output linked list after applying merge sort on the following linked list?
4 -> 2 -> 1 -> 3 -> None
medium
A. 1 -> 2 -> 3 -> 4 -> None
B. 4 -> 3 -> 2 -> 1 -> None
C. 2 -> 1 -> 3 -> 4 -> None
D. 1 -> 3 -> 2 -> 4 -> None
Solution
Step 1: Split the list into halves
Split into (4 -> 2) and (1 -> 3).
Step 2: Recursively sort and merge
Sort (4 -> 2) to (2 -> 4), sort (1 -> 3) to (1 -> 3), then merge to (1 -> 2 -> 3 -> 4).
Final Answer:
1 -> 2 -> 3 -> 4 -> None -> Option A
Quick Check:
Sorted ascending order = 1 -> 2 -> 3 -> 4 [OK]
Hint: Merge sort outputs sorted list in ascending order [OK]
Common Mistakes:
Confusing merge order and producing unsorted output
Not splitting list correctly
Merging without sorting halves first
4. Given this merge function snippet for linked lists, what is the bug?
def merge(left, right):
if not left:
return right
if not right:
return left
if left.val < right.val:
result = left
result.next = merge(left.next, right)
else:
result = right
result.next = merge(left, right.next)
return result
medium
A. The comparison should be <= instead of < to maintain stability.
B. No bug; function correctly merges two sorted lists.
C. The function does not handle empty lists properly.
D. The function should return None at the end instead of result.
Solution
Step 1: Analyze base cases
Returns right if left is None, and left if right is None, correctly handling empty lists.
Step 2: Check recursive merge logic
Compares values, assigns result, and recursively merges remaining nodes correctly.
Final Answer:
No bug; function correctly merges two sorted lists. -> Option B
Quick Check:
Merge function logic is correct and complete [OK]
Hint: Check base cases and recursion carefully for merge correctness [OK]
Common Mistakes:
Thinking <= is required for stability (not always needed)
Missing base case returns
Returning None instead of merged list
5. You have a linked list with nodes:
5 -> 3 -> 8 -> 2 -> 7 -> None
After applying merge sort, what will be the final linked list? Also, what is the time complexity of merge sort on this linked list?
hard
A. 2 -> 3 -> 5 -> 7 -> 8 -> None; Time complexity: O(n log n)
B. 2 -> 3 -> 5 -> 7 -> 8 -> None; Time complexity: O(n^2)
C. 5 -> 3 -> 8 -> 2 -> 7 -> None; Time complexity: O(n log n)
D. 3 -> 2 -> 5 -> 7 -> 8 -> None; Time complexity: O(n)
Solution
Step 1: Sort the linked list nodes
Sorted order is 2 -> 3 -> 5 -> 7 -> 8.
Step 2: Understand merge sort time complexity
Merge sort divides list and merges in O(n log n) time for n nodes.
Final Answer:
2 -> 3 -> 5 -> 7 -> 8 -> None; Time complexity: O(n log n) -> Option A
Quick Check:
Sorted list + merge sort time = O(n log n) [OK]
Hint: Merge sort always sorts in O(n log n) time [OK]
Common Mistakes:
Confusing time complexity with O(n^2)
Not sorting nodes correctly
Assuming merge sort is O(n)
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.