美文网首页
141-linked-list-cycle

141-linked-list-cycle

作者: Twopothead | 来源:发表于2019-07-28 23:21 被阅读0次
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
bool hasCycle(struct ListNode *head) {
    struct ListNode *p1 = head,*p2 = head;
    int count = 0,c=0;
    for(p1= head,c=0;p1!=NULL;p1 = p1->next,c++)
    {
        p2 = head;
        count = 0;
        while (p2 && (count++)<c)
        {
            if(p2 == p1)
                return true;
            p2=p2->next;
        }
    }
    return false;
}

网友评论

      本文标题:141-linked-list-cycle

      本文链接:https://www.haomeiwen.com/subject/qpvmrctx.html