This is not something which I would do in real life, but say:
LinkedList = a,b,c,d,e
and I get their corresponding index.
Say, I want to remove b (index=1) and d (index=3)
(i.e. values surrounding c (index=j=2)
)
Now,I do (which works fine):
When j=2
LS.remove(j + 1); ----> j=3 (d removed)
LS.remove(j - 1); ----> j=1 (b removed)
And b and d
are removed.
But if, I do (does not work):
When j=2
LS.remove(j - 1); ----> j=1 (b removed)
LS.remove(j); ----> j=2 (d is not removed) (used j because due to above removal, LL has adjusted it self)
i.e. when I move the value preceding 'c' first, 'd' is not removed and the LL stays as it is. I guess, I am doing the same thing.
Am I missing out on something here?
UPDATE:
So, when I change the signature public void operation(String operator, Integer j) to public void operation(String operator, int j)
, it worked.