Šimon Tóth’s Post

View profile for Šimon Tóth

C++ Educational Content Creator | 20 years of Software Engineering experience distilled into digestible daily posts

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

  • No alternative text description for this image

i use triple ref for automated insert and delete

Like
Reply

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.)

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?

Vladimir Sedlacek

Grandmaster 🧙♂️ of ICT, Grey hat, e-government consultant

3y

Null problemo. 😁

Vladimir Sedlacek

Grandmaster 🧙♂️ of ICT, Grey hat, e-government consultant

3y

Null problemo. 😁

See more comments

To view or add a comment, sign in

Explore content categories