1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public ListNode reverseList(ListNode head) { List l1 = new List(head); List l2 = new List(null); while (true) { ListNode first = l1.removeFirst(); if (first == null) break; l2.addFirst(first); } return l2.head; } static class List { ListNode head; public List(ListNode head) { this.head = head; } public void addFirst(ListNode first) { first.next = head; head = first; } public ListNode removeFirst() { ListNode h = head; if (head != null) { head = head.next; } return h; } }
|