# LEETCODE 876. Middle of the Linked List 解题思路分析

```输入：[1,2,3,4,5]

ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.```

```输入：[1,2,3,4,5,6]

• 给定链表的结点数介于 `1` 和 `100` 之间。

```ListNode res; // 返回结果
public ListNode middleNode(ListNode head) {
help(head, 0); // 递归
return res;
}
// head：当前节点
// index：当前下标
int help(ListNode head, int index){
int lastIndex; // 链表尾元素下标
// 如果当前元素是尾元素，lastIndex是当前下标
if(head.next==null) lastIndex = index;
// 否则递归至下一个元素，返回值为尾元素下标
else lastIndex=help(head.next, index+1);
// 如果当前元素是尾元素下标的中间值，当前节点即是返回结果
if(index==(lastIndex+1)/2) res = head;
// 返回尾元素下标
return lastIndex;
}```

Runtime: 0 ms, faster than 100.00% of Java online submissions for Middle of the Linked List.

Memory Usage: 36.7 MB, less than 7.84% of Java online submissions for Middle of the Linked List.

```public ListNode middleNode(ListNode head) {
ListNode slow=head, fast=head;
while(fast != null && fast.next != null){
slow=slow.next;
fast=fast.next.next;
}
return slow;
}```

Runtime: 0 ms, faster than 100.00% of Java online submissions for Middle of the Linked List.

Memory Usage: 36.7 MB, less than 7.84% of Java online submissions for Middle of the Linked List.