Skip to content

Instantly share code, notes, and snippets.

@Preetiraj3697
Forked from rahul4coding/getNode.js
Created October 21, 2022 05:41
Show Gist options
  • Save Preetiraj3697/b2ee29df2105d622bdb2766475f9017c to your computer and use it in GitHub Desktop.
Save Preetiraj3697/b2ee29df2105d622bdb2766475f9017c to your computer and use it in GitHub Desktop.
Get node value in a linked list position from tail | JS
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);
}
}
@Preetiraj3697
Copy link
Author

Get node value in a linked list position from tail | JS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment