求非空链表的中间节点,快慢指针法,双指针

/**

 * 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 p,q;

p=q=head;

while(q!=null&&q.next!=null){

    q=q.next.next;

    p=p.next;

}

 

return p;

 

    }

}

相关文章: