Skip to content

Instantly share code, notes, and snippets.

@rahul4coding
Created December 16, 2019 19:49
Show Gist options
  • Save rahul4coding/75e705a15b400e7ce4f64d0b0f0bcc6c to your computer and use it in GitHub Desktop.
Save rahul4coding/75e705a15b400e7ce4f64d0b0f0bcc6c 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment