Created
April 28, 2016 02:32
-
-
Save AntonisFK/7bbc15e4dbcd1399573c1df784879c41 to your computer and use it in GitHub Desktop.
Find the nth to last node in a linkedList
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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