Skip to content

Instantly share code, notes, and snippets.

@AntonisFK
Created April 28, 2016 02:32
Show Gist options
  • Save AntonisFK/7bbc15e4dbcd1399573c1df784879c41 to your computer and use it in GitHub Desktop.
Save AntonisFK/7bbc15e4dbcd1399573c1df784879c41 to your computer and use it in GitHub Desktop.
Find the nth to last node in a linkedList
function nthtoLast (SLL, n){
if(SLL.head === null){
return -1;
}else{
var len = 0,
current = SLL.head;
while(current.next !== null){
current = current.next;
len ++ ;
}
if( n > len){
return -1;
}else{
for(var i=0; i<=len-n+1; i++ ){
temp = temp.next;
}
return temp.value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment