if(first == null) { return;}
else {
Node c = first;
while(c!=null){
if(c.item == x && c.next!=null) {
c.next = c.next.next;
}
c = c.next;
}
}
This could be written more briefly as
for (Node c = first; c != null; c = c.next) {
if (c.item == x && c.next != null) {
c.next = c.next.next;
return;
}
}
You don't need the first if
, as the code has the same effect without it.
A for
loop better captures the iterative properties here.
If we return immediately after doing our task, we save some iterations.
Another alternative would be
if (first == null) {
return;
}
for (Node current = first; current.next != null; current = current.next) {
if (current.item == needle) {
current.next = current.next.next;
return;
}
}
I would prefer not to put everything on the same line. This would be even more true if there was an else
.
Since we return
, we don't need an else
. This has the same effect without it, and we save the extra indentation.
I find current
more descriptive than c
. Not a huge deal for a looping variable.
Since we know that first
is not null, we can go ahead and check the next
. This saves us an iteration if we're looking for the last item. This also saves having to check next
later.
I find needle
(meaning something for which we're searching) more descriptive than x
. That's a common idiom, but you can say target
, etc. if that doesn't work for you.
You might consider returning the value that you remove.
double value = current.next.item;
current.next = current.next.next;
return value;
This is common with remove
operations. You have the value right there at the time. And this saves having to traverse the list twice to get the item and then remove it. Of course, perhaps your usage patterns never have you doing that.