C ++ single-chain table inversion and single-chain table Inversion

Source: Internet
Author: User

C ++ single-chain table inversion and single-chain table Inversion

Notes for single-chain table reversal:

 1 #include<iostream> 2 #include<string.h> 3 using namespace std; 4  5 struct ListNode 6 { 7     int val; 8     ListNode* next; 9     ListNode(int i):val(i),next(NULL){};10 };11 void printList(ListNode* myList)12 {13     while(myList != NULL)14     {15         cout << myList -> val << "->";16         myList = myList -> next;17     }18     cout << "NULL" << endl;19 }20 ListNode* reverseList(ListNode* pHead)21 {22 23     if(pHead == NULL) return pHead;24     ListNode* pre = NULL;25     ListNode* cur = pHead;26     while(cur != NULL)27     {28        ListNode* newpre = cur;29        ListNode* newcur = cur -> next;30        cur -> next = pre;31        cur = newcur;32        pre = newpre;33     }34     return pre;35 }36 int main()37 {38     ListNode* pHead =  new ListNode(0);39     ListNode* cur = pHead;40     for (int i = 1; i < 10; ++i)41     {42         ListNode* newNode = new ListNode(i);43         cur -> next = newNode;44         cur = cur -> next;45     }46     printList(pHead);47     printList(reverseList(pHead));48     return 0;49 }

 

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.