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