Reverse part one-way link list __ List

Source: Internet
Author: User

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); }
}


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.