Reverse one-way and bidirectional linked list to simplify __ list

Source: Internet
Author: User
Tags prev static class

reverse one-way and bidirectional linked list simplification:

Reverse one-way and bidirectional linked list public class reverselist{//node definition public static class node{public int value;
        
          Public Node Next;

          public Node (int data) {this.value=data;
       Reverse one-way list public static Node Reverseonelist (Node head) {if (head==null| | head.next==null)
       {return head;
       Node Pre=null;

       Node Last=null;
        while (head!=null) {last=head.next;
       	Head.next=pre;
       	Pre=head;
       Head=last;
    return to pre;

    	 }//bidirectional linked list node definition public static class tnode{public int value;

    	 Public Tnode Prev,next;
    	 public tnode (int data) {this.value=data; //Reverse doubly linked list public static tnode reversetwolist (Tnode head2) {while head2==null| |
    	Head2.next==null) {return head2;
         } tnode Pre=null;
    	 Tnode Last=null;
    	while (Head2!=null) {last=head2.next;
            Head2.next=pre;
    	 	Head2.prev=pre;
    	 	Pre=head2;
    	 Head2=last;

    return to pre; }//Print list public static void Printlist (Node head) {while (head!=null) {System.out.print.
    		Value+ "");
    	Head=head.next;
    } System.out.println (); //print double linked list public static void PrintList2 (Tnode head) {while (head!=null) {System.out.print
    		Ad.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);
         Printlist (node);
         Node mode=reverseonelist (node);

         Printlist (mode);
          Bidirectional linked list inversion tnode tnode=new tnode (1);
          Tnode.next=new tnode (2);
          Tnode.next.next=new tnode (3);
          Tnode.next.prev=tnode;
          Tnode.next.next.prev=tnode.next; //printlist2 (tnode);
          Tnode t=reversetwolist (tnode);
   
	PrintList2 (t); }
}


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.