3
vector<vector<int>> spiralMatrix(int n, int m, ListNode *head) {4
// Create a matrix of n x m with values filled with -1.5
vector<vector<int>> spiral(n, vector<int>(m, -1));7
// Traverse the matrix in spiral form, and update with the values present in8
// the head list. If head reacher NULL pointer break out from the loop, and9
// return the spiral matrix.10
while (head != NULL) {12
while (head != NULL && j < m && spiral[i][j] == -1) {13
spiral[i][j] = head->val;17
if (head == NULL) break;22
while (head != NULL && i < n && spiral[i][j] == -1) {23
spiral[i][j] = head->val;31
while (head != NULL && j >= 0 && spiral[i][j] == -1) {32
spiral[i][j] = head->val;40
while (head != NULL && i >= 0 && spiral[i][j] == -1) {41
spiral[i][j] = head->val;51
// Rest values are itself -1.