RemoveK { public void remove(int k) { if (k < 0 || head == null) return; if (k == 0) { head = head.next; } else { ListNode kth = head; while (kth != null && k > 0) { kth = kth.next; k--; } if (kth != null) { ListNode prev = null; ListNode curr = head; while (kth != null) { prev = curr; curr = curr.next; kth = kth.next; } prev.next = curr.next; } } } RemoveK(ListNode head); void remove(int k); public ListNode head; }
@Test(dataProvider = "circleLL") public void testCircle(ListNode head, int k, List<Integer> array) { RemoveK solution = new RemoveK(head); solution.remove(k); assertLinkedListValues(solution.head, array); }