Watch 10 video solutions for Spiral Matrix IV, a medium level problem involving Array, Linked List, Matrix. This walkthrough by NeetCodeIO has 6,803 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
You are given two integers m and n, which represent the dimensions of a matrix.
You are also given the head of a linked list of integers.
Generate an m x n matrix that contains the integers in the linked list presented in spiral order (clockwise), starting from the top-left of the matrix. If there are remaining empty spaces, fill them with -1.
Return the generated matrix.
Example 1:
Input: m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0] Output: [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]] Explanation: The diagram above shows how the values are printed in the matrix. Note that the remaining spaces in the matrix are filled with -1.
Example 2:
Input: m = 1, n = 4, head = [0,1,2] Output: [[0,1,2,-1]] Explanation: The diagram above shows how the values are printed from left to right in the matrix. The last space in the matrix is set to -1.
Constraints:
1 <= m, n <= 1051 <= m * n <= 105[1, m * n].0 <= Node.val <= 1000Problem Overview: Given an m x n matrix and the head of a singly linked list, place the linked list values into the matrix in spiral order starting from the top-left corner. If the linked list runs out of values before the matrix is filled, the remaining cells must contain -1.
The task is essentially a spiral traversal simulation where values are consumed from a linked list and written into a matrix. The tricky part is managing boundaries correctly while stopping when the linked list is exhausted.
Approach 1: Boundary Triggered Spiral Traversal (O(mn) time, O(1) space)
Maintain four boundaries: top, bottom, left, and right. Traverse the matrix in four directions: left→right across the top row, top→bottom along the right column, right→left across the bottom row, and bottom→top along the left column. After each directional pass, shrink the corresponding boundary. During each step, assign the current linked list value and advance the pointer. If the list becomes null, fill remaining cells with -1. The matrix is filled exactly once, so the runtime is O(mn) with O(1) extra space beyond the output matrix.
This approach works well because spiral traversal naturally maps to shrinking boundaries. The logic is straightforward: write values while the current boundary range is valid and the linked list still has nodes.
Approach 2: Layer-by-Layer Spiral Traversal (O(mn) time, O(1) space)
Instead of triggering direction changes dynamically, treat the spiral as a sequence of layers. Each layer has four segments: top row, right column, bottom row, and left column. Iterate through each segment of the layer and place values from the linked list. After completing a layer, move inward by incrementing top and left, and decrementing bottom and right. Continue until the boundaries cross or the linked list ends.
The key idea is that each spiral loop forms a rectangular ring. Processing these rings sequentially keeps the logic structured and predictable. Like the previous method, each matrix cell is written once, giving O(mn) time and constant auxiliary space.
Both strategies rely on simple array indexing and direct simulation. No extra data structures are required besides the input linked list pointer.
Recommended for interviews: Boundary Triggered Spiral Traversal is the version most interviewers expect. It demonstrates control over spiral traversal patterns and careful boundary updates. The layer-by-layer approach shows the same understanding but organizes the logic around rectangular rings, which some developers find easier to reason about.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Boundary Triggered Spiral Traversal | O(mn) | O(1) | General solution for spiral matrix simulation; easiest to implement during interviews |
| Layer-by-Layer Spiral Traversal | O(mn) | O(1) | When you prefer structured traversal using concentric matrix layers |