Create and flip a one-way linked list

Source: Internet
Author: User

Thanks to XX's written examination, I finally made up my mind to thoroughly think about this seemingly simple question. I found my understanding was really unsatisfactory!

 

The following chain table flip is achieved by re-creating a new chain table. The space complexity is too low...

# Include <iostream> <br/> using namespace STD; <br/> struct node <br/> {<br/> int data; <br/> node * next; <br/>}* head; <br/> void addnode (INT value); <br/> void swap (node * & head); <br/> int main () <br/>{< br/> head = NULL; <br/> addnode (1); <br/> addnode (2); <br/> addnode (3 ); <br/> addnode (4); <br/> addnode (5); <br/> addnode (6); <br/> node * P = head; <br/> while (p) <br/>{< br/> cout <p-> data <"; <br/> P = p-> next; <br/>} <Br/> swap (head); <br/> P = head; <br/> cout <Endl; <br/> while (P) <br/>{< br/> cout <p-> data <""; <br/> P = p-> next; <br/>}< br/> system ("pause"); <br/> return 0; <br/>}< br/> void addnode (INT value) <br/>{< br/> node * P = new node (); <br/> P-> DATA = value; <br/> P-> next = NULL; </P> <p> If (! Head) <br/>{< br/> head = P; <br/>}< br/> else <br/>{< br/> node * q = head; <br/> while (Q-> next) <br/> q = Q-> next; <br/> q-> next = P; <br/>}< br/> void swap (node * & head) <br/>{< br/> node * qold = head; <br/> node * phead = new node (); <br/> If (! Head) <br/>{< br/> cout <"The linked list is empty! "<Endl; <br/> return; <br/>}< br/> phead-> DATA = head-> data; </P> <p> phead-> next = NULL; <br/> while (qold-> next) <br/>{< br/> node * pnew = new node (); <br/> * pnew = * (qold-> next ); <br/> pnew-> next = phead; <br/> phead = pnew; <br/> qold = qold-> next; <br/>}< br/> head = phead; <br/>}< br/>

Improvement: Use the legendary three-pointer method ~

Void swap (node * & head) <br/>{< br/> If (! Head) <br/>{< br/> cout <"The linked list is empty! "<Endl; <br/> return; <br/>}< br/> node * A [3]; <br/> A [0] = A [1] = NULL; <br/> A [2] = head; <br/> while (A [2]-> next) <br/>{< br/> A [0] = A [1]; <br/> A [1] = A [2]; <br/> A [2] = A [2]-> next; <br/> A [1]-> next = A [0]; </P> <p >}< br/> A [2]-> next = A [1]; <br/> head = A [2]; <br/>}

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.