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