1 classLnode2 {3 PublicLnode Next;4 Public intdata;5 }6 /*Reverse Linked list*/7 classNizhi8 {9 Private StaticLnode head =NewLnode ();Ten Private Staticlnode node; One Private StaticLnode tail; A Private Static intindex; - Private StaticLnode Newhead =NewLnode (); - Public Static voidMain (string[] args) { the int[] Nums = {1,2,3,4,5,6,7,8,9,10}; -Head.data = Nums[0]; -Tail =head; - Createline (nums); +System.out.println ("—————————————————— linked list order printing ———————————————————"); - PrintLine (head); + Nizhi (); ASystem.out.println ("—————————————————— linked list reverse print ———————————————————"); at PrintLine (newhead); - } - - Private Static voidCreateline (int[] nums) { - while(index<10) - { innode =NewLnode (); -Tail.next =node; toNode.data =Nums[index]; +Node.next =NULL; -Tail =node; theIndex + +; * $ }Panax Notoginseng } - the Private Static voidPrintLine (Lnode head) { +node =head; A while(node!=NULL&&node.next!=NULL){ thenode =Node.next; + System.out.println (node.data); - } $ } $ - Private Static voidNizhi () { - thenode =Head.next; - //traverse the original linked list node and head interpolation to the new head node .Wuyi while(node!=NULL) the { -Lnode temp =NewLnode (); WuTemp.data =Node.data; - //node.next = Node.next.next; AboutTemp.next =Newhead.next; $Newhead.next =temp; - -System.out.println ("Temp.data=" +temp.data); -node =Node.next; A } + } the}
Java implements single-linked list inversion