2326. Spiral Matrix IV LeetCode Solution
In this guide, you will get 2326. Spiral Matrix IV LeetCode Solution with the best time and space complexity. The solution to Spiral Matrix IV problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Spiral Matrix IV solution in C++
- Spiral Matrix IV solution in Java
- Spiral Matrix IV solution in Python
- Additional Resources

Problem Statement of Spiral Matrix IV
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 <= 105
1 <= m * n <= 105
The number of nodes in the list is in the range [1, m * n].
0 <= Node.val <= 1000
Complexity Analysis
- Time Complexity: O(mn)
- Space Complexity: O(mn)
2326. Spiral Matrix IV LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> spiralMatrix(int m, int n, ListNode* head) {
constexpr int dirs[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
vector<vector<int>> ans(m, vector<int>(n, -1));
int x = 0; // the current x position
int y = 0; // the current y position
int d = 0;
for (ListNode* curr = head; curr; curr = curr->next) {
ans[x][y] = curr->val;
if (x + dirs[d][0] < 0 || x + dirs[d][0] == m || y + dirs[d][1] < 0 ||
y + dirs[d][1] == n || ans[x + dirs[d][0]][y + dirs[d][1]] != -1)
d = (d + 1) % 4;
x += dirs[d][0];
y += dirs[d][1];
}
return ans;
}
};
/* code provided by PROGIEZ */
2326. Spiral Matrix IV LeetCode Solution in Java
class Solution {
public int[][] spiralMatrix(int m, int n, ListNode head) {
final int[][] dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int[][] ans = new int[m][n];
Arrays.stream(ans).forEach(A -> Arrays.fill(A, -1));
int x = 0; // the current x position
int y = 0; // the current y position
int d = 0;
for (ListNode curr = head; curr != null; curr = curr.next) {
ans[x][y] = curr.val;
if (x + dirs[d][0] < 0 || x + dirs[d][0] == m || y + dirs[d][1] < 0 || //
y + dirs[d][1] == n || ans[x + dirs[d][0]][y + dirs[d][1]] != -1)
d = (d + 1) % 4;
x += dirs[d][0];
y += dirs[d][1];
}
return ans;
}
}
// code provided by PROGIEZ
2326. Spiral Matrix IV LeetCode Solution in Python
class Solution:
def spiralMatrix(self, m: int, n: int, head: ListNode | None) -> list[list[int]]:
dirs = ((0, 1), (1, 0), (0, -1), (-1, 0))
ans = [[-1] * n for _ in range(m)]
x = 0 # the current x position
y = 0 # the current y position
d = 0
curr = head
while curr:
ans[x][y] = curr.val
if (x + dirs[d][0] < 0 or x + dirs[d][0] == m or y + dirs[d][1] < 0 or
y + dirs[d][1] == n or ans[x + dirs[d][0]][y + dirs[d][1]] != -1):
d = (d + 1) % 4
x += dirs[d][0]
y += dirs[d][1]
curr = curr.next
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.