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

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

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 pFlag;
}

设计一个不能被继承的类

利用虚继承的特性

#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 main()
{
    Son s;
}

只能在栈上生成对象

#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 Base;
}

只能在堆上生成对象

#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 main()
{
    Single_Hunger *s=Single_Hunger::getInstance();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值