Reverse Sequence of linked list

Source: Internet
Author: User
#include <iostream>using namespace std;struct Node{int nData;Node * pNext;};Node *CreateList(Node* pHead){Node * pTemp;for(int i = 1;i<10;i++){pTemp = new Node();pTemp->nData = i;pTemp->pNext = pHead;pHead = pTemp;}return pHead;}Node *ReverseList(Node* pHead){Node* p1 = pHead;Node* p2 = NULL;while(p1){Node * pTemp = p1->pNext;p1->pNext = p2;p2 = p1;p1 = pTemp;}return p2;}void ReverseList(Node** pHead){Node* p1 = *pHead;Node* p2 = NULL;while(p1){Node * pTemp = p1->pNext;p1->pNext = p2;p2 = p1;p1 = pTemp;}*pHead = p2;}Node * ReverseList_Digui(Node* pHead,Node* pHead2 = NULL){if(pHead == NULL)return pHead2;Node * pTemp = pHead->pNext;pHead->pNext = pHead2;return ReverseList_Digui(pTemp,pHead);}void Printf(Node* pHead){Node * pTemp = pHead;while(pTemp){cout<<pTemp->nData<<",";pTemp = pTemp->pNext;}cout<<endl;}int main(){Node * pHead;pHead = new Node();pHead->nData = 0;pHead->pNext=NULL;pHead = CreateList(pHead);Printf(pHead);pHead = ReverseList(pHead);Printf(pHead);ReverseList(&pHead);Printf(pHead);pHead = ReverseList_Digui(pHead);Printf(pHead);getchar();}

 

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.