首页 > 代码库 > 求单链表的中间节点,用快慢指针

求单链表的中间节点,用快慢指针

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Node* findMid(Node* &head)
{
    if(head == NULL||head->next == NULL)
        return head;
     
    Node* p = head;
    Node* q = head;
    while(q->next->next&&(q = q->next))
    {
        p = p-> next;
        q = q->next;
    }
    return p;
}