problemmediumalgorithmsleetcode-2326leetcode 2326leetcode2326spiral-matrix-ivspiral matrix ivspiralmatrixiv

Spiral Matrix 4 - From Linked List

MediumUpdated: Aug 2, 2025
Practice on:
Video Solutions:

Problem

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.

Examples

Example 1:

spiral-matrix-4-eg1.excalidraw

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.

Similar Problems

  • [Spiral Matrix 1 - Return](spiral-matrix-1-return)
  • [Spiral Matrix 2 - Generate](spiral-matrix-2-generate)
  • [Spiral Matrix 3 - Traverse from Given Starting Point](spiral-matrix-3-traverse-from-given-starting-point)

Solution

Method 1 - Iteration

We have to take following steps:

  1. Initialize the Matrix: Create an m x n matrix filled with -1.
  2. Traverse the Linked List: Insert integers from the linked list into the matrix in a spiral order.
  3. Handle Remaining Spaces: If the linked list is exhausted, continue to fill the remaining spaces with -1.

Video explanation

Here is the video explaining this method in detail. Please check it out:

<div class="youtube-embed"><iframe src="https://www.youtube.com/embed/fws_0jOdSoU" frameborder="0" allowfullscreen></iframe></div>

Code

Java
class Solution {
    public int[][] spiralMatrix(int m, int n, ListNode head) {
        int[][] matrix = new int[m][n];

        // Initialize matrix with -1
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matrix[i][j] = -1;
            }
        }

		int[][] directions = {
			{0, 1},  // Right
			{1, 0},  // Down
			{0, -1}, // Left
			{-1, 0}  // Up
		};

        int dirIdx = 0,
        r = 0,
        c = 0;
        ListNode current = head;

        while (current != null) {
            matrix[r][c] = current.val;
            current = current.next;

            int nextR = r + directions[dirIdx][0];
            int nextC = c + directions[dirIdx][1];

            if (nextR < 0 || nextR >= m || nextC < 0 || nextC >= n || matrix[nextR][nextC] != -1) {
                // Change direction
                dirIdx = (dirIdx + 1) % 4;
                nextR = r + directions[dirIdx][0];
                nextC = c + directions[dirIdx][1];
            }

            r = nextR;
            c = nextC;
        }

        return matrix;
    }
}

Complexity

  • ⏰ Time complexity: O(m * n)
  • 🧺 Space complexity: O(m * n)

Comments