struct ListNode{ int m_nKey; struct ListNode* m_pNext; };
ListNode *ReverseList(ListNode *phead) { //定义反转后链表的头结点 ListNode *pReverseHead=NULL; //指向当前结点的指针变量 ListNode *pNode=phead; //指向当前结点的前驱结点 ListNode *pPrevNode=NULL; while(pNode!=NULL) { //指向当前结点的后继结点 ListNode *pNext=pNode->m_pNext; if (pNext==NULL) { //当前结点为尾结点,则将其变成头结点 pReverseHead=pNode; } //让后面结点指向前面结点 pNode->m_pNext=pPrevNode; pPrevNode=pNode; pNode=pNext; } return pReverseHead; }
原文地址:http://blog.csdn.net/lsh_2013/article/details/45726063