@Test public void biggerInput() throws Exception { Solution solution = new Solution(); ListNode node = solution.oddEvenList(makeLinkedList(1, 2, 3, 4, 5, 6, 7, 8)); assertThat(node.val, is(1)); assertThat(node.val, is(3)); assertThat(node.val, is(5)); assertThat(node.val, is(7)); assertThat(node.val, is(2)); assertThat(node.val, is(4)); assertThat(node.val, is(6)); assertThat(node.val, is(8)); }
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);  }