@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.oddEvenList(one); assertThat(listNode.val, is(1)); assertThat(listNode.next.val, is(3)); assertThat(listNode.next.next.val, is(2)); }
public ListNode oddEvenList(ListNode head) { if (head == null || head.next == null) { return head; } else { int index = 3; ListNode lastOdd = head; ListNode lastEven = head.next; ListNode node = lastEven.next; while (node != null) { if (index % 2 == 1) { lastOdd.next = node; lastOdd = node; } else { lastEven.next = node; lastEven = node; } index++; node = node.next; } lastOdd.next = lastEven; return head; } }
Solution { public ListNode oddEvenList(ListNode head) { if (head == null || head.next == null) { return head; } else { int index = 3; ListNode lastOdd = head; ListNode lastEven = head.next; ListNode node = lastEven.next; while (node != null) { if (index % 2 == 1) { lastOdd.next = node; lastOdd = node; } else { lastEven.next = node; lastEven = node; } index++; node = node.next; } lastOdd.next = lastEven; return head; } } }
Solution { public ListNode oddEvenList(ListNode head) { if (head == null || head.next == null) { return head; } else { int index = 3; ListNode lastOdd = head; ListNode lastEven = head.next; ListNode node = lastEven.next; while (node != null) { if (index % 2 == 1) { lastOdd.next = node; lastOdd = node; } else { lastEven.next = node; lastEven = node; } index++; node = node.next; } lastOdd.next = lastEven; return head; } }  }
Solution { public ListNode oddEvenList(ListNode head) { if (head == null || head.next == null) { return head; } else { int index = 3; ListNode lastOdd = head; ListNode lastEven = head.next; ListNode node = lastEven.next; while (node != null) { if (index % 2 == 1) { lastOdd.next = node; lastOdd = node; } else { lastEven.next = node; lastEven = node; } index++; node = node.next; } lastOdd.next = lastEven; return head; } }  ListNode oddEvenList(ListNode head); }
Solution { public ListNode oddEvenList(ListNode head) { if (head == null || head.next == null) { return head; } else { int index = 3; ListNode lastOdd = head; ListNode lastEven = head.next; ListNode node = lastEven.next; while (node != null) { if (index % 2 == 1) { lastOdd.next = node; lastOdd = node; } else { lastEven.next = node; lastEven = node; } index++; node = node.next; } lastOdd.next = lastEven; return head; } }  ListNode oddEvenList(ListNode head);  }