It is meaningless to use the 2 pointers technique, you won't get benefit from that, it still has the same dereference compare to 1 pointer 2 pass.
You want to have cache benefit when n is small?
I hope leetcode can remove this misleading question because it encourages those people to use this "technique" to "optimize" code. It is silly.
I Agree. This problem is meaningless.
There are at least three solution:
- get the length of list in the first pass, remove the element in the second pass
- use two pointers. keep the distance equal to n (stupid, no performance benefit, hard to program)
- use recursion. remove the element when exiting from the recursion (very stupid, linear space usage)