Sunday common C++ interview problem: Removing the n-th element from the back of a singly-linked list. Given a singly-linked list, remove the n-th element from the end of the list. You should do this in a single pass and only with constant additional memory. Solve it yourself: https://lnkd.in/evC7dwMc Solution: https://lnkd.in/eSsxj9Q9 #cpp #cplusplus #coding #programming #dailybiteofcpp
Your solution is not a single pass one, sorry. I'm afraid it is indistinguishable from a solution that just counts length-N and does the second pass to find the right element.
I really wonder about the exact definition of the requirement "single pass". I would say, it's not wrong if somebody interprets it as "you can issue only one read operation against the elements". (Which interpretation makes the given solution invalid) The interpretation of "with constant additional memory" is also a bit vague. Clearly it shouldn't depend on the length of the list, but can it depend on n? (If yes, that of course saves the day related to the "single pass" issue.)
Anil Kumar solve this
Next should be, you are given a linked list with pointer to one of its elements which is not the last element. You have to delete that element. You are NOT provided the head pointer. How would you do it?
Where is the catch?
Null problemo. 😁
Null problemo. 😁
i use triple ref for automated insert and delete