淘先锋技术网

首页 1 2 3 4 5 6 7
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        
        if(head == NULL || head->next == NULL)
            return head;
        ListNode *fast,*slow;
        fast=head->next;slow=head;
        while(fast != NULL)
        {
            slow=slow->next;
            if(fast->next != NULL)
                fast=fast->next->next;
            else
                break;

        }
        return slow;
    }
};