7.7 Remove k-th last element from a list

#1

According to the problem description we should “not assume that it is possible to record the length of the list”. I want to verify that this restriction does not mean that it’s impossible to know when we’ve reached the end of the list. Rather it only means the numeric distance traversed would be too large to store in memory. Is this interpretation correct?

0 Likes

#2

Yes, I would say that your assumption is correct. Else, we would not be able to compute the kth last element.

0 Likes

#3

Thanks @testUser.

Another question - May I assume that, since the number k is stored in memory, the restriction on memory storage does not prevent me from introducting a count variable that can store any number up to k? So, since k is an integer, may I store any 32 bit number?

0 Likes

#4

yep, but that may not be always helpful since your list could be arbitrarily long and you want to return kth element from the last which would be (n-k) from the beginning of the list.

1 Like

#5

@testUser If I can store the number k, then I believe I can find the k-th last element using two pointers.

0 Likes