-
-
Save Preetiraj3697/b2ee29df2105d622bdb2766475f9017c to your computer and use it in GitHub Desktop.
Get node value in a linked list position from tail | JS
This file contains hidden or 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 getNode(head, positionFromTail) { | |
var length =0; | |
var tp = head; | |
while(tp!==null){ | |
length++; | |
tp=tp.next | |
} | |
let currentPosition=0; | |
if(head==null){ | |
return head; | |
}else{ | |
while(currentPosition < length-positionFromTail-1 && head.next!==null){ | |
head=head.next; | |
currentPosition++; | |
} | |
return(head.data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Get node value in a linked list position from tail | JS