publicstatic Node reverseList(Node head){ Node p = new Node(0); Node q = new Node(0); Node r = new Node(0); p = head; q = head.next; p.next = null;
while(q!=null){ r = q.next; q.next = p; p = q; q = r; } head = p; return head; }
publicstaticvoidmain(String[] args){ int count = 9; Node t = new Node(1); Node x = t; for(int i = 2; i <= count; i++){ x = (x.next = new Node(i)); } t = reverseList(t); while(t!=null){ System.out.print(t.val+" "); t = t.next; } } publicstaticclassNode{ int val; Node next; Node(int v){ val = v; } } }
近期评论