Matrix Spiral
Function Name: spiralOrder
Description
Write a function that takes an m x n matrix of integers as an input and returns a list of the matrix's elements in spiral order. Spiral order starts at the top-left corner of the matrix, goes to the right, and proceeds in a spiral pattern all the way until every element has been visited.
Requirements
- The function should take one parameter, which is a 2D list (list of lists) representing the matrix.
- Assume the matrix will have at least one element.
- Do not use any built-in language methods that solve the problem directly.
- You can assume the input matrix is a non-empty rectangular grid of numbers.
Examples
Example 1: spiralOrder([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) should return [1, 2, 3, 6, 9, 8, 7, 4, 5].Example 2: spiralOrder([[1], [2], [3]]) should return [1, 2, 3].Example 3: spiralOrder([[1, 2, 3, 4]]) should return [1, 2, 3, 4].Example 4: spiralOrder([[1, 2], [3, 4], [5, 6], [7, 8]]) should return [1, 2, 4, 6, 8, 7, 5, 3].
Links
Pathfinder
Fri Dec 06 2024
Implement a function 'findShortestPath' that takes a grid (2D array) consisting of '0's (empty spaces), '1's (walls), 'S' (start), and 'E' (end).The function should return an integer representing the length of the shortest path from 'S' to 'E' that only passes through empty spaces ('0').If no path exists, the function should return -1.The path can only move horizontally or vertically, not diagonally.
Sorting Rail Cars
Sun Dec 08 2024
Imagine a train sorting system where rail cars containing numbers need to be reordered according to their values. Each rail car is linked to the next, forming a chain. Your task is to write a function that receives a linked list representing a train of rail cars, and returns a linked list with the cars sorted in ascending order. You cannot use auxiliary arrays, pointers, or new data structures; the existing linked list must be manipulated to sort the cars.