public void reverse(SinglyList<T> list) { Node<T> p = list.head.next, succ = null, front = null; while (p != null) { succ = p.next;//设置succ是p结点的后继结点,即用succ来保持p的后继结点 p.next = front;//逆转,即使p.next指向p结点的前驱结点 front = p;//front向后移一步 p = succ;//p向后移一步 } list.head.next = front;//head指向原链表的最后一个结点,完成逆转 }
public class SinglyList_reverse { public static <T> SinglyList<T> createReverse(T[] values) { SinglyList<T> list = new SinglyList<T>(values); return list; } public static void main(String[] args) { String[] values = {"A", "B", "C", "D", "E", "F"}; SinglyList<String> list = createReverse(values); System.out.println("list" + list.toString()); SinglyList s = new SinglyList(); s.reverse(list); System.out.println(",逆转后" + list.toString()); } }