What is the main idea behind merge sort when sorting a linked list?
Merge sort divides the linked list into two halves, sorts each half recursively, 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 sorted lists efficiently by changing pointers, while quicksort relies on random access which linked lists do not support efficiently.
Click to reveal answer
beginner
In merge sort for linked lists, how do you find the middle point to split the list?
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
beginner
How does merging two sorted linked lists work in merge sort?
Compare the head nodes of both lists, attach the smaller one to the result, and move that list's pointer forward. Repeat until all nodes are merged into a sorted list.
Click to reveal answer
What technique is used to find the middle of a linked list in merge sort?
ACounting nodes then dividing by two
BRandom access by index
CUsing a stack
DTwo pointers: slow and fast
✗ Incorrect
Two pointers, slow and fast, are used because linked lists do not support random access. The fast pointer moves twice as fast as the slow pointer to find the middle.
What is the best time complexity of merge sort on a linked list?
AO(log n)
BO(n^2)
CO(n log n)
DO(n)
✗ Incorrect
Merge sort always runs in O(n log n) time for linked lists, as it divides the list and merges sorted halves.
Why is merge sort efficient for linked lists compared to quicksort?
AMerge sort changes pointers without extra space
BMerge sort requires extra arrays
CQuicksort is faster on linked lists
DMerge sort uses random access
✗ Incorrect
Merge sort merges by changing pointers directly, which suits linked lists. Quicksort needs random access, which linked lists lack.
What happens after splitting the linked list in merge sort?
AReverse the halves
BSort each half recursively
CMerge without sorting
DDelete one half
✗ Incorrect
Each half is sorted recursively before merging back together.
How do you merge two sorted linked lists?
ACompare heads and link smaller node first
BSort both lists again
CAppend one list to the other
DUse an array to merge
✗ Incorrect
Merging compares the head nodes and links the smaller one first, continuing until all nodes are merged.
Explain step-by-step how merge sort sorts a linked list.
Think about dividing the list and then combining sorted parts.
You got /4 concepts.
Describe why merge sort is a good choice for sorting linked lists compared to other sorting algorithms.
Consider how linked lists store data and how merge sort works with pointers.
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. Merge sort is faster than quicksort in all cases.
B. Merge sort uses less memory than quicksort.
C. Merge sort does not require random access, which suits linked lists well.
D. Merge sort sorts the list 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 well with sequential access by splitting and merging, while quicksort relies on random access for partitioning.
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 avoids random access [OK]
Common Mistakes:
Thinking quicksort is always faster
Assuming merge sort sorts in place without extra space
Confusing memory usage between merge sort and quicksort
2. Which Rust code snippet correctly defines a singly linked list node struct for merge sort?
easy
A. struct ListNode { val: i32, next: Box<ListNode> }
B. struct ListNode { val: i32, next: Option }
C. struct ListNode { val: i32, next: &ListNode }
D. struct ListNode { val: i32, next: Option<&ListNode> }
Solution
Step 1: Understand ownership in Rust linked lists
Rust requires ownership for heap data; Box<T> owns heap data, Option allows None or a node.
Step 2: Analyze options
struct ListNode { val: i32, next: Option } uses Option which is correct for nullable next pointer. Others use references without ownership or missing Option.