📖 Scenario: You work at a company that manages a list of tasks. Each task is stored as a node in a linked list. The tasks are currently unordered, and you need to sort them by their priority number to help the team work efficiently.
🎯 Goal: Build a Rust program that creates a linked list of tasks, then sorts the list using the merge sort algorithm. Finally, print the sorted list showing task priorities in order.
📋 What You'll Learn
Create a singly linked list with nodes containing integer priorities
Implement a function to split the linked list into halves
Implement the merge sort algorithm to sort the linked list by priority
Print the sorted linked list priorities in order
💡 Why This Matters
🌍 Real World
Sorting linked lists is useful in many applications like task scheduling, memory management, and real-time data processing where data is naturally linked.
💼 Career
Understanding linked list sorting and merge sort is important for software engineers working on system programming, embedded systems, and performance-critical applications.
Progress0 / 4 steps
1
Create the Linked List with Task Priorities
Create a singly linked list struct called ListNode with a field priority: i32 and a next pointer. Then create a linked list called head with these exact priorities in order: 4, 2, 5, 1, 3.
DSA Rust
Hint
Define a struct with priority and next. Use Box to create the linked list nodes. Link nodes by setting next fields.
2
Add a Helper Function to Print the Linked List
Add a function called print_list that takes a reference to Option<Box<ListNode>> and prints the priorities in order separated by -> and ending with null.
DSA Rust
Hint
Use a while let loop to traverse the list. Print each priority followed by -> . End with null.
3
Implement Merge Sort for the Linked List
Implement a function called merge_sort that takes Option<Box<ListNode>> and returns a sorted Option<Box<ListNode>> by priority using merge sort. Also implement helper functions split_list to split the list into halves and merge to merge two sorted lists.
DSA Rust
Hint
Use the fast and slow pointer technique in split_list to find the middle. Recursively sort left and right halves in merge_sort. Merge sorted halves in merge.
4
Sort the List and Print the Sorted Priorities
Call merge_sort on the linked list head and store the result in sorted_head. Then call print_list with &sorted_head to print the sorted priorities.
DSA Rust
Hint
Call merge_sort on Some(head) and save to sorted_head. Then call print_list(&sorted_head) to print the sorted list.
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.