【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;
}

 


posted @ 2018-08-16 13:18  丁壮  阅读(216)  评论(0编辑  收藏  举报