@Test public void shouldReverseLinkedListIterative() { ListNode<Integer> head = new ListNode<Integer>(1); ListNode<Integer> n2 = new ListNode<Integer>(2); ListNode<Integer> n3 = new ListNode<Integer>(3); ListNode<Integer> n4 = new ListNode<Integer>(4); head.setNext(n2); n2.setNext(n3); n3.setNext(n4); ListNode reversedList = reverseLinkedList.reverseIterative(head); assertEquals(n4, reversedList); assertEquals(n3, reversedList.getNext()); assertEquals(n2, reversedList.getNext().getNext()); assertEquals(head, reversedList.getNext().getNext().getNext()); }
public ListNode reverseIterative(ListNode list) { validateInput(list); if (list.getNext() == null) { return list; } ListNode head = null; ListNode current = list; while (current != null) { ListNode save = current; current = current.getNext(); save.setNext(head); head = save; } return head; }
ReverseLinkedList { public ListNode reverseIterative(ListNode list) { validateInput(list); if (list.getNext() == null) { return list; } ListNode head = null; ListNode current = list; while (current != null) { ListNode save = current; current = current.getNext(); save.setNext(head); head = save; } return head; } }
ReverseLinkedList { public ListNode reverseIterative(ListNode list) { validateInput(list); if (list.getNext() == null) { return list; } ListNode head = null; ListNode current = list; while (current != null) { ListNode save = current; current = current.getNext(); save.setNext(head); head = save; } return head; }  }
ReverseLinkedList { public ListNode reverseIterative(ListNode list) { validateInput(list); if (list.getNext() == null) { return list; } ListNode head = null; ListNode current = list; while (current != null) { ListNode save = current; current = current.getNext(); save.setNext(head); head = save; } return head; }  ListNode reverseIterative(ListNode list); ListNode<Integer> reverseRecursive(ListNode list); }
ReverseLinkedList { public ListNode reverseIterative(ListNode list) { validateInput(list); if (list.getNext() == null) { return list; } ListNode head = null; ListNode current = list; while (current != null) { ListNode save = current; current = current.getNext(); save.setNext(head); head = save; } return head; }  ListNode reverseIterative(ListNode list); ListNode<Integer> reverseRecursive(ListNode list);  }