分析:定义两个指针,分别为fast和slow指针,fast每次向后走两步,slow 每次向后走一步,直到fast走完,slow下标所对应位置的数就是要返回节点的数。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow=head;
ListNode fast=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
}