<p>You are given two integers <code>m</code> and <code>n</code>, which represent the dimensions of a matrix.</p>

<p>You are also given the <code>head</code> of a linked list of integers.</p>

<p>Generate an <code>m x n</code> matrix that contains the integers in the linked list presented in <strong>spiral</strong> order <strong>(clockwise)</strong>, starting from the <strong>top-left</strong> of the matrix. If there are remaining empty spaces, fill them with <code>-1</code>.</p>

<p>Return <em>the generated matrix</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/09/ex1new.jpg" style="width: 240px; height: 150px;" />
<pre>
<strong>Input:</strong> m = 3, n = 5, head = [3,0,2,6,8,1,7,9,4,2,5,5,0]
<strong>Output:</strong> [[3,0,2,6,8],[5,0,-1,-1,1],[5,2,4,9,7]]
<strong>Explanation:</strong> The diagram above shows how the values are printed in the matrix.
Note that the remaining spaces in the matrix are filled with -1.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/11/ex2.jpg" style="width: 221px; height: 60px;" />
<pre>
<strong>Input:</strong> m = 1, n = 4, head = [0,1,2]
<strong>Output:</strong> [[0,1,2,-1]]
<strong>Explanation:</strong> 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.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m, n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= m * n &lt;= 10<sup>5</sup></code></li>
	<li>The number of nodes in the list is in the range <code>[1, m * n]</code>.</li>
	<li><code>0 &lt;= Node.val &lt;= 1000</code></li>
</ul>

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def spiralMatrix(self, m: int, n: int, head: Optional[ListNode]) -> List[List[int]]:
 
        #create the n * m matrix 
        output = [[-1] * n for _ in range(m)]

        #create by east, south, west, north matrix. #if you wanted to go a different direction first you can just change the order of this haha!
        directions = [[0, 1], [1, 0], [0, -1], [-1, 0]]

        #current direction (east)
        curr_direction = 0 

        #declare row and col 
        row = 0 
        col = 0

        #iterate through linked list 
        while head: 
            #fill the current location with the current node's value 
            print(f"({row}, {col})")
            output[row][col] = head.val

            #increment i and j by the current direction, first is for i second is for j 
            newrow = row + directions[curr_direction][0]
            newcol = col + directions[curr_direction][1]

            #if i and j are out of bounds then change direction 
            if ( 
                #if either are less than 0 
                min(newrow, newcol) < 0
                #if either reach their bounds
                or newrow >= m 
                or newcol >= n 
                #if the new place has already been visited 
                or output[newrow][newcol] != -1
            ):
                curr_direction = (curr_direction + 1) % 4 # 0 is east 1 is south 2 is west and 3 is north 

            #increment row and col by the correct current direction
            row += directions[curr_direction][0]
            col += directions[curr_direction][1]

            #advance the node pointer 
            head = head.next

        #return the output 
        return output 

        
       
        
'''
understand: 
- Alright so I want to try a different approach for the spiral matrix because 
there's a lot of moving parts to keeping track of the bounds and a bit of mental 
math that I don't really appreciate. 
- I found it really intuitive yesterday for Spiral Matrix III to just increment 
i,j to the next valid place. if I could just find a clever way of doing this then
i might find a better more concise and elegant way fo doing what I have done! 

match: 
- okay the strategy that I would like to complete today is one where I move i,j
in the correct direction until I have to switch directions and this case or until 
I finish iterating through the list 
- I can basically store the east, south, west and north movements in a matrix, 
and add those numbers to the original i and j every time I want to move down the
linked list. This will work because for example to move east I could just store
[0, 1] and do i+ 0 and j + 1 which would move this pointer to the right! 
- once I hit an edge or something where I'd need to change direction, I need to move
in the next clockwise direction. I can use remainders 0, 1, 2, 3, to represent 
these four directions, and have a curr_direction counter that I increment by one
when needed. then I will get the next appropriate direction by nature of modulus 
stuff which is great


'''
55400