Inverse partial one-way linked list of algorithm summary

Source: Internet
Author: User

The header node of the given single-linked list head, and two integers from and to, invert the fro node to the first node on a unidirectional list

Ideas:

There may be a problem with the header, so the function should return the adjusted new head node

1 determine if the 1<=from<=to<=n satisfies the original head node directly if not satisfied

2 Find the From-1 node pre and the To+1 node tpos,fpre that is to reverse the previous node of the part, TPOs is the reverse part of the next node, reverse the reverse part first, and then the correct link fpre and TPOs

 PackageTT;ImportTT. Test85.node; Public classTest90 { PublicNode Reversepart (node head,intFrom,intTo ) {        intLen = 0; Node Node1=Head; Node Fpre=NULL; Node TPos=NULL; while(node1!=NULL) {len++; Fpre= Len = = from-1?Node1:fpre; TPos= len== to+1?Node1:tpos; Node1=Node1.next; }        if(from<to | | from<1 | | to>Len) {            returnHead; } Node1= fpre==NULL?Head:fPre.next; Node Node2=Node1.next; Node1.next=TPos; Node Next=NULL;  while(Node2! =TPos) {Next=Node2.next; Node2.next=Node1; Node1=Node2; Node2=Next; }        if(Fpre! =NULL) {Fpre.next=Node1; returnHead; }        returnNode1; }            }

Inverse partial one-way linked list of algorithm summary

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.