Intermediate Level

Implement the reorderLinkedListValues method that reorders linked list values in first-last alternating order.

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

Your task is to reorder the list in this pattern: first node, last node, second node, second last node, and so on.

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

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

Use two pointers to pick values from both ends of the list.

Take one value from the beginning, then one value from the end. Move the left pointer forward and the right pointer backward. Repeat this process until all values are added to the result.

If the list has an odd number of nodes, the middle value will be added once when both pointers meet.

Pseudocode:

function reorderLinkedListValues(nodes, size):
    result = empty array
    left = 0
    right = size - 1
    while left <= right:
        append nodes[left] to result
        if left != right:
            append nodes[right] to result
        left++
        right--
    return result
Run your code to see the result.