876. 链表的中间结点
问题描述

问题分析
代码实现
js
var middleNode = function(head) {
let count = 0
let temp = head
while(temp) {
count++
temp = temp.next
}
for(let i = 0; i < (count-1)/2; i++) {
head = head.next
}
return head
};
java
class Solution {
public ListNode middleNode(ListNode head) {
if (head.next == null) return head;
if (head.next.next == null) return head.next;
ListNode slow = head.next;
ListNode fast = head.next.next;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == null) return slow;
}
return slow;
}
}
树下留言
LET’S TALK文字是一次相遇。很高兴听到你的声音。