Given an matrix, return one list containing all of its elements in clockwise spiral order. Begin by traversing the top row from left to right, continue down the rightmost column, traverse the bottom row from right to left, and then move up the leftmost column before repeating the process for the remaining inner matrix.
Implement the following operation:
spiral_order(matrix)
Input:
[[1, 2, 3], [4, 5, 6]]
Output:
[1, 2, 3, 6, 5, 4]
The traversal visits the top row, the right edge, and then the bottom row in reverse order.
Input:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Output:
[1, 2, 3, 6, 9, 8, 7, 4, 5]
After the outer ring is consumed, only the center value remains to be appended.
Input:
[[1], [2], [3]]
Output:
[1, 2, 3]
A one-column matrix is read from top to bottom without revisiting any element.