I'm writing an extra removeRange()
method that except a start index
and end index
as parameters. I've passed all the conditions except when the number of nodes equals the range from start to end.
example my linked list contains:
1 -> 2 -> 3
after the method removeRange(0,2)
is called:
list should become null
, since from 0 to 2, the count is 3 and there are also 3 elements in my list.
look at the picture for a better idea of what's going on if you can.

Code:
public void removeRange(int start, int end) {
if(start < 0 || end < 0) {
throw new IllegalArgumentException();
}
if(start == 0 && end == 0) {
front = front.next;
} else if (start == 0 && end == 1) {
front = front.next.next;
} else {
ListNode head = front;
for(int i = 0; i < start-1;i++) {
head = head.next;
}
ListNode tail = front;
for(int i = 0; i < end;i++) {
tail = tail.next;
}
head.next = tail.next;
}
}
Assuming that your tail.next == null
then you still need to set front = head
as you have done similarly in your previous cases where you update front
.