Java version linked list reverse
Define data Structures:
/** * Data structure of the linked list */class Linkedlistarray { /** * value */ Object value; /** * Next node */ Linkedlistarray next = null; public void SetValue (Object value) { this.value = value; } public void Setnext (Linkedlistarray next) { this.next = next; } Public Object GetValue () { return value; } Public Linkedlistarray GetNext () { return next; }}
Link List Inverse method:
/** * Created by Hyson on 15/7/14. */public class Listreverse {public static void main (string[] args) {Linkedlistarray llr1 = new Linkedlistarray (); Llr1.setvalue ("Chain"); Linkedlistarray LLR2 = new Linkedlistarray (); Llr2.setvalue ("table"); Linkedlistarray LLR3 = new Linkedlistarray (); Llr3.setvalue ("inverse"); Linkedlistarray LLR4 = new Linkedlistarray (); Llr4.setvalue ("place"); Llr1.setnext (LLR2); Llr2.setnext (LLR3); Llr3.setnext (LLR4); Print (LLR1); LLR1 = reverse (LLR1); Print (LLR1); public static void print (Linkedlistarray head) {linkedlistarray tmp = head; while (tmp! = null) {System.out.print (Tmp.getvalue ()); TMP = Tmp.next; } System.out.println (); }/** * List reverse * @param listhead Header * @return Reverse list * */public static Linkedlistarray reverse (Linkedlis Tarray listhead) {linkedlistarray lastlist,//remaining linked list Tmplist,//Temporary list result;//results linked list reference lastlist = Listhead; Tmplist = result = null;//The staging list is initialized with the result set to empty while (lastlist! = null) {tmplist = lastlist.next;//The staging list is staged in addition to the first node The remaining list Lastlist.next = result;//puts the first node into the remaining list header result = lastlist;//reference to the first position of the list lastlist = tmp list;//remaining list reference re-points to the remaining list of persisted lists} return result; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linked list reverse for Java