Intermediate Level

Implement the sortLinkedListValues method that sorts linked list values and returns the sorted list.

The linked list is provided as an integer array nodes, where each value represents one node value.

Your task is to return all linked-list values in ascending order. The returned array should contain the same values, only sorted from smallest to largest.

For example, [4,2,1,3] becomes [1,2,3,4].

Example 1
Input:
nodes (int[]) = [4,2,1,3]
size (int) = 4
Return:
(int[]) [1,2,3,4]
Example 2
Input:
nodes (int[]) = [-1,5,3,4,0]
size (int) = 5
Return:
(int[]) [-1,0,3,4,5]
Example 3
Input:
nodes (int[]) = []
size (int) = 0
Return:
(int[]) []

Since this practice input is already provided as an array, the direct solution is to sort the array values.

In a real linked-list problem, merge sort is commonly used because it works well with linked lists. Here, we only need to return the sorted values, so sorting the array representation gives the required result.

If the list is empty, return an empty array.

Pseudocode:

function sortLinkedListValues(nodes, size):
    result = copy of nodes
    sort result in ascending order
    return result
Run your code to see the result.