输入一个链表的头结点,反转该链表,并输出反转后链表的头结点。
1 struct ListNode 2 { 3 int m_nValue; 4 ListNode* m_pNext; 5 };
1 ListNode* ReverseList(ListNode* pHead) 2 { 3 ListNode* pReverseHead = NULL; 4 ListNode* pNode = pHead ; 5 ListNode* pPrev = NULL; 6 while (pNode!=NULL) 7 { 8 ListNode* pNext = pNode->m_pNext ; 9 if (pNext == NULL) 10 { 11 pReverseHead = pNode; 12 } 13 pNode->m_pNext = pPrev ; 14 pPrev = pNode ; 15 pNode = pNext ; 16 } 17 18 return pReverseHead; 19 }