【leetcode 简单】 第三十四题 环形链表

给定一个链表,判断链表中是否有环。

进阶:
你能否不使用额外空间解决此题?


/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    struct ListNode *pfast,*pslow;
    if(NULL == head || head->next == NULL)
    {
        return false;
    }
    pfast = pslow = head;
    while(pfast->next != NULL && pfast->next->next !=NULL)
    {
        pfast = pfast->next->next;
        pslow = pslow->next;
        if(pfast == pslow)
        {
            return true;
        }
    }
    return false;
}

猜你喜欢

转载自www.cnblogs.com/flashBoxer/p/9486711.html
今日推荐