//链表定义
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;
}
逆置/反转单链表+查找单链表的倒数第k个节点
最新推荐文章于 2023-09-17 10:12:18 发布