Practice: Merge K Sorted Lists
Problem: https://leetcode.com/problems/merge-k-sorted-lists/
Recognition reminder: you have k sorted chains and must combine them into one sorted chain. “Merge k of anything sorted” is where the merge primitive scales up and the heap pattern arrives early.
Before you start (the five-beat rhythm)
- Your Pattern Card for this week is already written.
- Name the pattern aloud and write your approach as a plain-English comment before any code.
- Struggle floor: 25 minutes unaided. No hints, no AI, no Discuss tab.
- If stuck past the floor, ask the tutor for a hint. Six rungs, one per ask.
- Debrief in your commit message before moving to the next problem.
Your target
Fill these in yourself before you look at anyone else’s solution:
Target time complexity: ____
Target space complexity: ____
Merging the lists two at a time works but is not the best you can do. A min-heap holding the current head of every list lets you always pull the global smallest next. Think about how many nodes the heap holds at once, and remember the tuple-entry tiebreaker from the refresher so two equal values never force a comparison of two ListNode objects.
Where your code goes
Write your solution in your own work repo (see getting-started.md), not in this folder. Your solution.py must define the same ListNode class LeetCode gives you (val, next) and a function merge_k_lists(lists) that takes a list of list-heads (some of which may be None) and returns the head of the single merged list. This folder ships only the problem spec and a provided-example test (tests/test_provided.py) so you can check the given cases locally before you submit to LeetCode’s judge. The judge is the oracle; the tutor will not confirm your answer by reading it.
Debrief (paste into your commit message)
1. What pattern did this turn out to be?
2. What was the trigger phrase or input shape that should have made me reach for it?
3. What was the time and space complexity, and what would dominate at scale?
4. What edge case would have broken my first attempt?
5. What would I do differently in three days when I see this cold?