reverse Partial one-way list:
To invert the public class reversesublist{//Linked list node of a partial one-way list the definition of public static class node{public int value;
Public Node Next;
Node's definition public node (int data) {this.value=data; //Inverse partial list public static node Reversesublist (Node head, int from,int to) {if head==null| |from <0| |
to<0) {return head;
Node P=head;
int leng=0; Node Tpre=null; Gets the previous node tpos=null of the reverse start node;
Gets the last node of the inverse End node//the calculated list length while (p!=null) {++leng; Tpre= (Leng==from-1?p:tpre); Finds the previous node tpos= (leng==to+1?p:tpos) of the Reverse start node;
Finds the last node of the reverse end node P=p.next; } if (From>leng| | To>leng| |
From>to) {return head; } P=tpre==null?head:tpre.next;
Locate node Node2=p.next to start the reversal; P.next=tpos;
The node at the beginning of reversal points to the second node of the reverse end node next=null;
while (Node2!=tpos) { Next=node2.next;
node2.next=p;
P=node2;
Node2=next;
} if (tpre!=null) {tpre.next=p;
return head;
} return p; }//Print list public static void Printlist (Node head) {while (head!=null) {System.out.print
. value+ "");
Head=head.next;
} System.out.println ();
public static void Main (String []args) {Node node=new node (1);
Node.next=new Node (2);
Node.next.next=new Node (3);
Node.next.next.next=new Node (4);
Node.next.next.next.next=new Node (5);
Printlist (node); Node mode=reversesublist (node,2,4);
Reverse Node 2---4 printlist (mode); }
}