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

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


/**
 * 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;
}

 


相关文章:

  • 2021-10-01
  • 2022-12-23
  • 2022-12-23
  • 2018-10-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-04-26
猜你喜欢
  • 2022-12-23
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
相关资源
相似解决方案