/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
//当head和head->next有值的时候,!1 = 0
if(head == nullptr || head->next == nullptr)
{
return false;
}
if(head == head->next)
{
return true;
}
head->next = head->next->next;
return hasCycle(head->next);
}
};
leetcode141 环形链表通过递归算法求解(很妙的递归算法)
最新推荐文章于 2024-06-09 20:18:37 发布