本文主要是介绍leetcode141 环形链表通过递归算法求解(很妙的递归算法),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/**
* 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 环形链表通过递归算法求解(很妙的递归算法)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!