Java implements one-way reverse linked list, and java implements reverse
The examples in this article share the code for Java to implement one-way linked list inversion for your reference. The details are as follows:
1. Implementation Code
Public class implements listtest {public static void main (String [] args) {Node A = new Node ("A"); Node B = new Node ("B "); node C = new Node ("C"); Node D = new Node ("D"); Node E = new Node ("E "); node F = new Node ("F");. next = B; B. next = C; C. next = D; D. next = E; E. next = F; print (A); Response listreversor reversor = Response listreversor. RECURSION; System. out. println (reversor. getStrategy () + ":"); Node t Mp = reversor.exe cute (A); print (tmp); reversor = policlistreversor. NO_RECURSION; System. out. println (reversor. getStrategy () + ":"); print(reversor.exe cute (tmp);} private static void print (Node node) {while (Node! = Null) {System. out. print (node. value); node = node. next; if (node! = Null) {System. out. print ("->");} else {System. out. println () ;}}} class Node {public String value; public Node next; public Node (String value) {this. value = value ;}} enum implements listreversor {RECURSION ("RECURSION") {@ Override public Node execute (Node node) {Node prev = null; if (node = null | node. next = null) {prev = node;} else {Node tmp = execute (node. next); node. next. next = node; node. n Ext = null; prev = tmp;} return prev ;}}, NO_RECURSION ("non-recursive") {@ Override public Node execute (Node node Node) {Node prev = null; while (node! = Null) {Node tmp = node; node = node. next; tmp. next = prev; prev = tmp;} return prev ;}}; private String strategy; private inclulistreversor (String strategy) {this. strategy = strategy;} public abstract Node execute (Node node); public String getStrategy () {return strategy ;}}
2. Implementation ideas
Recursion: processing from the end
Non-recursion: processing from the header
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.