Learner Level

Implement the middleNodeLinkedList method that returns the value stored at the middle node of the linked list.

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

Your task is to return the value stored at the middle node of the linked list. If the list has an even number of nodes, return the second middle node.

For example, in [1,2,3,4,5], the middle value is 3. In [1,2,3,4,5,6], the two middle values are 3 and 4, so the answer is 4.

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

In a real linked list, this is commonly solved using the slow and fast pointer method. The slow pointer moves one node at a time, and the fast pointer moves two nodes at a time. When the fast pointer reaches the end, the slow pointer is at the middle node.

In this problem, the linked list is represented as an array, so we can directly calculate the middle index as size / 2. Integer division gives the correct second middle index for even-sized lists.

Pseudocode:

function middleNodeLinkedList(nodes, size):
    middleIndex = size / 2
    return nodes[middleIndex]
Run your code to see the result.