Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Nash_Cyk/article/details/79083066
List in reverse order is relatively common operation, pay attention to the link off the chain! Note the empty state judge!
LIST_NODE * ListNodeReverse(LIST_NODE * m_pHead)
{
if (m_pHead == NULL)
{
return NULL;
}
if (m_pHead->next == NULL)
{
return m_pHead;
}
LIST_NODE * m_pReverse = NULL;
LIST_NODE * m_pCurrent = m_pHead;
while(m_pCurrent != NULL)
{
LIST_NODE * pTemp = m_pCurrent; //保存当前节点
m_pCurrent = m_pCurrent->next; //将当前节点向下移动
pTemp->next = m_pReverse; //将保存的节点连接·到反向链表的头
m_pReverse = pTemp; //将链表接上
}
return m_pReverse;
}