Node.java:
. 1 Package com.me.node; 2 . 3 / ** . 4 * list of node objects . 5 * @author wuyizuokan . 6 * . 7 * @param <T> . 8 * / . 9 public class the Node <T> { 10 / * Node stored value * / . 11 Private T value; 12 is / * a next reference node * / 13 is Private the node <T> next; 14 15 public T the getValue () { 16 return value; . 17 } 18 public void setValue(T value) { 19 this.value = value; 20 } 21 public Node<T> getNext() { 22 return next; 23 } 24 public void setNext(Node<T> next) { 25 this.next = next; 26 } 27 }
NodeList.java:
. 1 Package com.me.node; 2 . 3 / ** . 4 * list implementation class . 5 * @author wuyizuokan . 6 * . 7 * @param <T> . 8 * / . 9 public class a NodeList <T> { 10 . 11 / * list head node * / 12 is Private the Node <T> head; 13 is 14 / ** 15 * elements are added to the list 16 * @param T . 17 * / 18 is public void add(T t) { 19 Node<T> node = new Node<T>(); 20 node.setValue(t); 21 if (head == null) { 22 head = node; 23 } else { 24 add(head, node); 25 } 26 } 27 28 private void add(Node<T> node, Node<T> newNode) { 29 if (node.getNext() == null) { 30 node.setNext(newNode); 31 } The else { 32 the Add (node.getNext (), the newNode); 33 is } 34 is } 35 36 / ** 37 [ * The reverse packet list 38 is * @param I 39 * / 40 public void reverseGroup ( int groupSize) { 41 is head = reverseGroup (head, groupSize); 42 is } 43 is 44 is Private the Node <T> reverseGroup (the Node <T> Node, int I) { 45 //splitNode taken for i-th element before the array, for the following cycle will splitNode moved to a position of the i-th node 46 is the Node <T> = splitNode Node; 47 for ( int J =. 1; J <i; J ++ ) { 48 IF (splitNode =! null ) { 49 splitNode = splitNode.getNext (); 50 } the else { 51 is BREAK ; 52 is } 53 is } 54 is // if moved to the position of the i-th node, the node is null, the description can not be achieved the size of a group, then reversed and returned directly 55 IF (splitNode == null ) { 56 is return reversList (Node); 57 is } 58 // Mark the rest of the head of the list for the next batch packet 59 the Node <T> = nextListHead splitNode.getNext (); 60 // set splitNode next node is null, to the list before the i-th node extracted. 61 is splitNode.setNext ( null ); 62 is // call list inversion method is reversed. 63 is the Node <T> = newHead reversList (Node); 64 // recursive call group according to the method of reversing the array 65 the Node <T> = nextListHeadTmp reverseGroup (nextListHead, I); 66 // reverse to return the next sub list stitching to the back this time. 67 node.setNext (nextListHeadTmp); 68 // return the head node for the new list. 69 return newHead; 70 } 71 is 72 / ** 73 is * inverted list 74 * / 75 public void reversList () { 76 head = reversList (head); 77 } 78 79 Private the Node <T> reversList (the Node <T> Node ) { 80 // herein as recursive outlet, corresponding to the linked list is recursively split by node, until the last node to split the 81 IF (node == null || node.getNext () == null ) { 82 return Node; 83 } 84 85 // recursive call list inversion method, a new list of return header 86 the Node <T> = Result reversList (node.getNext ()); 87 // because it is recursive, node where each node is actually from the beginning of the penultimate node onward. It needs to be for the next 88 // next node point to itself. 89 node.getNext () SetNext (Node);. 90 // his own blanking the next node, after the entire function actually finished execution, in addition to the next node before the first node to be a reference space, no other nodes meeting. 91 is node.setNext ( null ); 92 return Result; 93 } 94 95 public void ShowList () { 96 System.out.print("["); 97 if (head != null) { 98 showNode(head); 99 } 100 System.out.println("]"); 101 } 102 103 public void showNode(Node<T> node) { 104 if (node != null) { 105 System.out.print(node.getValue()); 106 if (node.getNext() != null) { 107 System.out.print(", "); 108 showNode(node.getNext()); 109 } 110 } 111 } 112 }
Test code:
1 package com.me.reverseLinkedList; 2 3 import com.me.node.NodeList; 4 5 public class Test { 6 public static void main(String[] args) { 7 NodeList<String> nodeList = new NodeList<String>(); 8 nodeList.showList(); 9 nodeList.add("a"); 10 nodeList.add("b"); 11 nodeList.add("c"); 12 nodeList.add("d"); 13 nodeList.add ( "E" ); 14 nodeList.add ( "F" ); 15 nodeList.add ( "G" ); 16 nodeList.add ( "H" ); . 17 18 is nodeList.showList (); . 19 // inverted list 20 is nodeList.reversList (); 21 is nodeList.showList (); 22 is // packet inverted list 23 is nodeList.reverseGroup (. 3 ); 24 nodeList.showList (); 25 } 26 is }
running result: