400. Construct a Linked List from a 2D Matrix in Spiral Order

HardLinked ListMatrixSimulationLinked List

Given a 2D matrix, construct a linked list whose nodes follow the spiral order of the matrix (clockwise, starting at the top-left). Return the linked list as an array of values in spiral order. The matrix is given as a JSON array of rows.

Input: JSON array of rows (the matrix).

Output: Array — values in spiral order.

Examples

Example 1
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
Explanation: Clockwise spiral.
Example 2
Input: [[1,2,3,4]]
Output: [1,2,3,4]
Explanation: Single row.
Example 3
Input: [[1],[2],[3]]
Output: [1,2,3]
Explanation: Single column.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →