public class linked{ private Note first; public void add(T t) { if (first == null) { first = new Note<>(t); return; } Note note = first; while (note != null) { if (note.next == null) { note.next = new Note<>(t); return; } else { note = note.next; } } } public Note getHead() { return first; } public T getT(Note note) { return note.t; } public void reversal() { //没数据或者只有一个数据的直接返回 if (first == null || first.next == null) return; Note preNote = null; Note curNote = first; Note tem;//记录下次循环的开头节点 while (curNote != null) { tem = curNote.next; curNote.next = preNote; preNote = curNote; curNote = tem; } first = preNote; } public void reversal(Note head) { if (head == null) return; if (head.next == null) { first = head; return;//可以返回最后节点,变成链表新的头节点 } Note next = head.next; reversal(next); next.next = head; head.next = null; } public void printf() { Note note = this.first; while (note != null) { System.out.println(note.getT().toString()); note = note.next; } } private static class Note { private E t; private Note next; public E getT() { return t; } public Note(E t) { this.t = t; } public Note getNext() { return next; } } }



