How to realize recursive inversion of single-linked table
The main content of this article is "how to achieve single-linked table recursive inversion", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to realize the recursive inversion of a single-linked table.
Public class Node {private String name = null; private Node next=null; public Node (String name,Node next) {this.name = name; this.next=next;} / / Recursive reverse node public Node reverse (Node head) {if (head==null | | head.next= = null) {return head;} Node temp = head.next; Node newHead = reverse (head.next); temp.next=head; head.next=null; return newHead } / / traverse the names of all nodes public String getNodeAllName () {String names = ""; Node currNode = this; while (currNodekeeper null) {names + = currNode.name+ "- >"; currNode = currNode.next;} return names;} public static void main (String [] args) {Node nodeC = new Node ("C", null) Node nodeB = new Node ("B", nodeC); Node nodeA = new Node ("A", nodeB); System.out.println (nodeA.getNodeAllName ()); / / reverse A node Node reversedNode = nodeA.reverse (nodeA); System.out.println (reversedNode.getNodeAllName ()) }} at this point, I believe you have a deeper understanding of "how to achieve single-linked table recursive inversion". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!