xxxxxxxxxx
35
class LinkedListNode {
constructor(value){
this.value = value;
this.next = null;
}
}
function setup() {
const a = new LinkedListNode('Angel Food');
const b = new LinkedListNode('Bundt');
const c = new LinkedListNode('Cheese');
const d = new LinkedListNode(`Devil's Food`);
const e = new LinkedListNode('Eccles');
a.next = b;
b.next = c;
c.next = d;
d.next = e;
let solution = kthToLastNode(0,a);
console.log(solution.value );
}
function kthToLastNode(k,node){
// lets make a list of all the nodes in order
let nexts = [];
let next = node;
while (next != null){
next = next.next;
append(nexts,next);
}
return nexts[nexts.length-k-2];
}