leetcode 141 Linked List Cycle C++

来源:互联网 发布:装修业务网络单 编辑:程序博客网 时间:2024/06/05 16:37

利用了追赶的原理


class Solution {public:    bool hasCycle(ListNode *head) {        ListNode *fastNode = head;        while(fastNode){            head = head->next;            if(fastNode->next != NULL) fastNode = fastNode->next->next;            else return false;            if(head == fastNode) return true;        }        return false;    }};


0 0
原创粉丝点击