Reverse (also called reverse) of the linked list

Source: Internet
Author: User

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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.