题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。链表结点定义如下:
struct listNode
{
int m_nKey;
listNode * m_pNext;
};
/*
Function: reverseList
Description: reverse the single list
Input:
[head]: the list of head pointer
Output:
Return: the head pointer of reverse list
Others: null list return NULL pointer
*/
listNode * reverseList( listNode * pHead )
{
if( NULL == pHead ) return NULL;
listNode * pNext = pHead->m_pNext;
listNode * pPrev = NULL;
while( NULL != pNext )
{
pHead->m_pNext = pPrev;
pPrev = pHead;
pHead = pNext;
pNext = pNext->m_pNext;
}
return pHead;
}