單鏈表逆置,反轉,並查找倒數第K個結點

typedef struct Node

{

    Node(const int& value)

    : m_value(value)

    , m_pNext(NULL)

    {}

    int   m_value;

    Node* m_pNext;

}Node,

pNode ResList(pNode pHead)
{
    if (pHead == NULL)
        return NULL;
    if (pHead->m_pNext == NULL)
        return pHead;
         pNode pNewHead = NULL;
    pNode pCur = pHead->m_pNext;
    while (pHead)
    {
        pHead->m_pNext = pNewHead;
        pNewHead = pHead;
        pHead = pCur;
        if (pCur)
            pCur = pCur->m_pNext;
    }
    return pNewHead;
}

pNode Find(pNode* pHead,int k)
{
    if (*pHead == NULL)
        return NULL;
    pNode pLinkHead = *pHead;
    for (int i = 0; i < k - 1; i++)
    {
        pLinkHead = pLinkHead->m_pNext;
    }
    pNode pBehind = *pHead;
while (pLinkHead->m_pNext!=NULL)
    {
        pLinkHead = pLinkHead->m_pNext;
        pBehind = pBehind->m_pNext;
    }
    return pBehind;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章