Spiral Matrix IV

Medium Topics Company Tags

Create an m by n matrix using the values of a singly linked list, beginning at head. Start at the top-left cell, move right across the top row, and continue clockwise around the outside before filling the inner layers.

Place list values in their original order along this spiral. When the list ends, leave every unfilled cell as -1. Return the matrix.

The input array named head represents linked-list values from head to tail. Your function receives the linked-list head, not an array.

Example 1:

Spiral Matrix IV, example 1: illustration of the example

Input: m = 2
n = 3
head = [1,2,3,4]

Output: [[1,2,3],[-1,-1,4]]

Explanation: The first three values fill the top row from left to right. The next value, 4, goes below the top-right corner. The two remaining cells stay -1.

Example 2:

Spiral Matrix IV, example 2: illustration of the example

Input: m = 3
n = 2
head = [0,7,0,8,9,6]

Output: [[0,7],[6,0],[9,8]]

Explanation: The list fills the top row, continues down the right column, then moves left and up. Zero is a list value and is written like any other value.

Example 3:

Spiral Matrix IV, example 3: illustration of the example

Input: m = 2
n = 2
head = [5]

Output: [[5,-1],[-1,-1]]

Explanation: Only the top-left cell receives 5. The list then ends, so the other three cells remain -1.

Constraints:

  • 1 <= m, n <= 100000
  • 1 <= m × n <= 100000
  • The list contains between 1 and m × n nodes.
  • 0 <= Node.val <= 1000


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.