# Implement an algorithm to find the Kth to last element of a singly linked string

I wrote this algorithm to find the Kth element of a singly linked list and I would like your feedback because I want to know if there is a better version.

In my eyes, this is the most optimized way of doing it and I've covered every edge case (I also assumed that the only possible input is a number, if I removed that assumption, I would add another if block), but I really would appreciate your feedback.

PS: I also only added these two main methods (add and findLast) because I figured that there were the only two required to find a solution, but I'm not sure if I should add more in an interview (i.e. remove, removeAt, addAt, isEmpty, etc.)

function LinkedList() {
let length = 0;

function Node(element) {
this.element = element;
this.next = null;
}

this.length = function() {
return length;
}

}

let node = new Node(element);

} else {

while (currentNode.next) {
currentNode = currentNode.next;
}
currentNode.next = node;
}
length++;
}

this.findLast = function(index) {
if(index > length) {
return false;
}
if (index < 0) {
return false;
}

let realIndexes = length-1; // if length = 5, realIndexes = 4
let realPosition = index-1; // if index = 2, realPosition = 1
// We are looking for the real position here.
let realTarget = realIndexes - realPosition;
let counter = 0;

while (counter < realTarget) {
currentNode = currentNode.next;
counter++;
}
return currentNode.element;
}
}

console.log(congo)
congo.length()


Output: => 5

## migrated from stackoverflow.comOct 15 '18 at 3:59

This question came from our site for professional and enthusiast programmers.

• Okay thank you Mark, wasn't sure if Stack Overflow was right for this – Abdel Shokair Oct 3 '18 at 20:12
• Are you sure the task is / was not to find the k-th element from the end? – Stefan Haustein Oct 3 '18 at 20:14
• No, it's not really optimal: don't put the Node constructor inside the LinkedList one, use prototype methods instead of per-instance ones, and for optimisation purposes also store a reference to the last node in the list instance. – Bergi Oct 3 '18 at 20:23
• realIndexes and realPosition don't make any sense. What was wrong with the normal ones? Notice that realTarget==realIndexes - realPosition==length - index. – Bergi Oct 3 '18 at 20:27
• "I'm not sure if I should add more in an interview" - that's rather off-topic here. But no, in an interview you should implement only what is necessary for the task to solve. No (good) interviewer would ask you to implement a "complete" LinkedList class. – Bergi Oct 3 '18 at 20:28

To get the sub-list of the last n elements without computing the length first (see comments):

• Go ahead with an end pointer until you have skipped n elements.
• Now go ahead in parallel with a start pointer and the end pointer until the end pointer reaches the end of the list (the start and end pointers will stay n elements apart as you go).
• Now start points to the desired sub-list.

In an interview, before you implement anything, it's often a good idea to first describe your plan. Counting first works and is no different in terms of the O notation, but I doubt it's what the interviewer wanted to see. Describing your plan can avoid spending time on something the interviewer might not really be interested in.

## Unsafe code

We can only guess what the interviewer is looking for. Usually they just want to know if you can write code in whatever language is required.

So the following is just to point out a danger in the code. Should you change your code? I can't say..

## Protect state

Your function is flawed and can easily have its state corrupted. This is because you expose Node.next and don't consider the various problems this will cause. eg

const list = new LinkedList();


Delete second item from the list

head.next = head.next.next;


Now the length you store is incorrect and the result of findLast will either return index - 1, index + 1, or worse throw if you reach the end as you will have assigned null to currentNode in the previous step and currentNode.next will throw Can not read property "next" of null

head.next = head; // now you have a infinit linked list

There function LinkedList.add(data) will not exit.