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

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

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();
}
posted @ 2017-07-16 23:14  乐天的java  阅读(51)  评论(0)    收藏  举报