1 #include "stdafx.h" 2 #include <iostream> 3 /* 4 题目:反转链表 5 定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。链表结点定义如下: 6 struct ListNode 7 { 8 int m_nKey; 9 ListNode* m_pNext; 10 }; 11 */ 12 using namespace std; 13 struct ListNode 14 { 15 int m_nKey; 16 ListNode* m_pNext; 17 }; 18 ListNode* ReverseList(ListNode* pHead) 19 { 20 ListNode* pReversedHead = NULL; 21 ListNode* pNode = pHead; 22 ListNode* pPrev = NULL; 23 while(pNode!=NULL) 24 { 25 ListNode* pNext = pNode ->m_pNext; 26 if(pNext==NULL) 27 pReversedHead = pNode; 28 pNode->m_pNext=pPrev; 29 pPrev = pNode; 30 pNode = pNext; 31 } 32 return pReversedHead; 33 34 } 35 int _tmain(int argc, _TCHAR* argv[]) 36 { 37 38 return 0 ; 39 }