Solution { public ListNode reverseList(ListNode head) { if (head == null) { return null; } List<ListNode> nodes = new ArrayList<>(); nodes.add(head); while (head.next != null) { nodes.add(head.next); head = head.next; } for (int i = nodes.size() - 1; i >= 0; i--) { if (i == 0) { nodes.get(i).next = null; } else { nodes.get(i).next = nodes.get(i - 1); } } return nodes.get(nodes.size() - 1); }  ListNode reverseList(ListNode head);  }
@Test public void shouldReverse() throws Exception { ListNode one = new ListNode(1); ListNode two = new ListNode(2); ListNode three = new ListNode(3); one.next = two; two.next = three; Solution solution = new Solution(); ListNode listNode = solution.reverseList(one); assertThat(listNode.val, is(3)); assertThat(listNode.next.val, is(2)); assertThat(listNode.next.next.val, is(1)); }