leetcode 141

张开发
2026/4/19 13:10:58 15 分钟阅读

分享文章

leetcode 141
如果有环快指针一定可以追上慢指针。如果没有环快指针一定可以指向nullptrnullptrnullptr从而结束循环。classSolution{public:boolhasCycle(ListNode*head){ListNode*slowhead;ListNode*fasthead;if(headnullptr||head-nextnullptr){returnfalse;}while(fast!nullptrfast-next!nullptr){slowslow-next;fastfast-next-next;if(slowfast){returntrue;}}returnfalse;}};

更多文章