Kth element from last in linked list


What is the Error in My Code?

See we need to find the kth element from last without computing the length of the linked list…
for ex if the list is
1 2 3 4 5 6 7
k=2
then output should be 6
please modify your logic…
also in one file only one class can be public…so remove public from class node(make it default)

What is the error in my code now?

Please check as soon as possible and let me know.

see you are not getting the logic of the ques…please read what i have written in prev comment…
if the input is k=2 and list=1->2->3->4->5->-1
then lastIndexOf(2) will return 1 as element at index 1 is 2…but we have to return the 2nd last element of the list that is 4

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.