Reverse (also called reverse) of the linked list
The inverse of the linked list is often used as the interview questions for fresh graduates. It mainly examines the understanding of the linked list and the thinking ability of job seekers. The idea of reverse placement is mainly to save several temporary pointer variables. In fact, many questions can be solved by saving the temporary variables.
The C ++ code is as follows:
#include "stdafx.h"struct ListNode{ int m_nData; ListNode* m_pNext;};ListNode* ReverseList(ListNode *pHead){ ListNode* pReversedHead = NULL; ListNode* pNode = pHead; ListNode* pPrev = NULL; while(NULL != pNode) { ListNode* pNext = pNode->m_pNext; if (NULL == pNext) pReversedHead = pNode; pNode->m_pNext = pPrev; pPrev = pNode; pNode = pNext; } return pReversedHead;}int _tmain(int argc, _TCHAR* argv[]){ int len = 10; ListNode *pHead = new ListNode; pHead->m_nData = 10; pHead->m_pNext = NULL; ListNode *pPrev = pHead; for (int i=0; i<len; i++) { ListNode *p = new ListNode; p->m_nData = i; p->m_pNext = NULL; if (NULL != pPrev) { pPrev->m_pNext = p; } pPrev = p; } ListNode *pReversedHead = ReverseList(pHead); return 0;}