← All problems

Spiral Matrix

Medium matrixsimulation LeetCode ↗
Given an m x n matrix, return all its elements in spiral order, starting at the top-left and moving clockwise inward.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(mn), Space O(1) excluding output

Common pitfalls

  • Double-visiting the last row/column
  • Off-by-one on boundaries

Your solution

Log in to save your code and track progress.