SwapPairs { public static <T> ListNode<T> swapWithExtraNode(ListNode<T> head) { if (head == null || head.next == null) { return head; } ListNode<T> extra = new ListNode(0); ListNode<T> prev = extra; prev.next = head; ListNode<T> current = head; while (current != null && current.next != null) { prev.next = current.next; current.next = current.next.next; prev.next.next = current; current = current.next; prev = prev.next.next; } return extra.next; }  static ListNode<T> swap(ListNode<T> head); static ListNode<T> swapWithExtraNode(ListNode<T> head);  }
@Test(dataProvider = "linkedList") public void testSwapWithExtra(ListNode<Integer> original, ListNode<Integer> expected) { TestUtilities.assertEquals(SwapPairs.swapWithExtraNode(original), expected); }