0
点赞
收藏
分享

微信扫一扫

求带环链表入口点 计算时间复杂度

码农K 2022-10-13 阅读 141


求带环链表入口点 计算时间复杂度

ListNode *MeetNode(ListNode *&pHead)
{
if (pHead == nullptr&&pHead->_pNext==nullptr)
return nullptr;
ListNode *pSlow = pHead->_pNext;
ListNode *pFast;
if (pSlow->_pNext)
pFast = pSlow->_pNext;
while (pSlow&&pFast)
{
if (pFast == pSlow)
return pSlow;
pSlow = pSlow->_pNext;
pFast = pFast->_pNext;
if (pFast)
pFast = pFast->_pNext;
}
return nullptr;
}
ListNode *EntryNodeofLoop(ListNode *pHead)
{
ListNode *meetNode = MeetNode(pHead);
if (meetNode == nullptr)
return nullptr;
int count = 1;
ListNode *pFlag = meetNode;
while (pFlag->_pNext != meetNode)
{
pFlag = pFlag->_pNext;
count++;
}
pFlag = pHead;
for (int i = 0; i < count; i++)
pFlag = pFlag->_pNext;
ListNode *pNode = pHead;
while (pNode != pFlag)
{
pNode = pNode->_pNext;
pFlag = pFlag->_pNext;
}
return

设计一个不能被继承的类

利用虚继承的特性

#include<iostream>
using namespace std;
class Grand
{
friend class Parent;
private:
Grand()
{}
~Grand()
{}
};
class Parent:virtual public Grand
{
public:
Parent()
{}
~Parent()
{}
};
class Son :public Parent
{};
int

只能在栈上生成对象

#include<iostream>
using namespace std;
class Base
{
public:
Base()
{}
~Base()
{}
private:
void *operator new(size_t);
void operator delete(void *);
};
int main()
{
Base *b = new

只能在堆上生成对象

#include<iostream>
using namespace std;
class Single_Hunger
{
public:
static Single_Hunger *getInstance()
{
return newInstance;
}
static Single_Hunger *newInstance;
private:
Single_Hunger()
{}
};
Single_Hunger *Single_Hunger::newInstance = new Single_Hunger;
int


举报

相关推荐

0 条评论