遂宁建设网站,wordpress+模板宽度,洛阳网站建站,衡阳网站建设设计链接#xff1a;
141. 环形链表
题意#xff1a;
求链表是否有环
解#xff1a;
刚好昨天做完的初级算法链表题#xff0c;翻转和暴力
实际代码#xff1a;
#includeiostream
using namespace std;
struct ListNode
{int val;ListNode *next;ListNode() : …链接
141. 环形链表
题意
求链表是否有环
解
刚好昨天做完的初级算法链表题翻转和暴力
实际代码
#includeiostream
using namespace std;
struct ListNode
{int val;ListNode *next;ListNode() : val(0), next(nullptr) {}ListNode(int x) : val(x), next(nullptr) {}ListNode(int x, ListNode *next) : val(x), next(next) {}
};
bool hasCycle(ListNode *head)//翻转法
{if(headnullptr) return false;ListNode* newheadnullptr,* oldheadhead;for(;head!nullptr;){ListNode* temphead-next;head-nextnewhead;newheadhead;headtemp;}if(oldheadnewheadnewhead-next!nullptr) return true;return false;
}
/*
bool hasCycle(ListNode *head)//暴力法
{if(headnullptr) return false;int n0;while(head-next!nullptr){headhead-next;n;if(n10007) return true;}return false;
}*/
int main()
{}限制
链表中节点的数目范围是 [0, 104]-105 Node.val 105pos 为 -1 或者链表中的一个 有效索引 。