Given a matrix represented as a list of lists of integers, return its transpose. The transpose of a matrix swaps rows and columns: the element at row i, column j moves to row j, column i. The input matrix may not be square (rows and columns can differ in count). Return null for null input and an empty list for an empty matrix.

Examples:

Input: matrix = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]] Explanation: 2x3 matrix becomes 3x2 after transpose Input: matrix = [[1,2],[3,4],[5,6]] Output: [[1,3,5],[2,4,6]] Explanation: 3x2 matrix becomes 2x3 after transpose Input: matrix = [[1]] Output: [[1]] Explanation: Single element matrix transposes to itself Input: matrix = null Output: null Explanation: Null input returns null Input: matrix = [] Output: [] Explanation: Empty matrix returns empty list
Apex Code Editor

Welcome to Lightning Challenge!

Create an Account

Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.

Create account

How It Works

  • • Write your solution in the code editor
  • • Connect your Salesforce org to test
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 All Time Leaderboard

Contest runs January 1, 1970 - December 31, 9999. Complete challenges to climb the leaderboard!

Points are calculated for challenges completed during the contest period.