Manafall Daily Quest

Sorting Rail Cars

Function Name: sortRailCars

Description

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.

Requirements

  • The 'sortRailCars' function must take one parameter, which is the head of a linked list.
  • Each node in the linked list has an 'intValue' representing the number on the rail car, and a 'next' reference pointing to the next car.
  • The function must sort the linked list in ascending order by manipulating the nodes' 'next' references.
  • Do not create any new nodes or linked lists.
  • You may not use any built-in sorting methods.
  • The linked list may not be converted into any other data structure for sorting.
  • The function must be able to handle a linked list of at least 1,000 nodes without performance issues.
  • Do not use recursion; an iterative solution is expected due to potential stack overflow concerns with large inputs.

Examples

Given a linked list: 4 -> 1 -> 3 -> 2, after calling 'sortRailCars(head)', the list should be: 1 -> 2 -> 3 -> 4.

Links

https://en.wikipedia.org/wiki/Linked_listhttps://www.geeksforgeeks.org/linked-list-set-1-introduction/

Matrix Spiral

Sat Dec 07 2024

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.

Prev Quest

Binary Tree Right Side View

Mon Dec 09 2024

Given the root of a binary tree, imagine yourself standing on the right side of the tree, return the values of the nodes you can see ordered from top to bottom.The binary tree's TreeNode is defined as: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };The function should traverse the tree level by level and only take the farthest right node's value at each level.The tree may contain duplicate values.

Next Quest