Middle of the Linked List
Given a non-empty, singly linked list with head node head, return a middle
node of linked list.
If there are two middle nodes, return the second middle node.
//注意链表结点的个数奇数还是偶数
//偶数结点个数,low指向后半部分的第一个
//奇数结点个数,low指向越过对称中心结点,指向后半部分的第一个结点
参考:快慢指针的slow节点的位置问题 就很简单了
class Solution {
public ListNode middleNode(ListNode head) {
if(head==null||head.next==null){
return head;
}
ListNode fast=head;
ListNode slow=head;
while(fast!=null&&fast.next!=null){
fast=fast.next.next;
slow=slow.next;
}
return slow;
}
}