Created
December 16, 2019 19:49
-
-
Save rahul4coding/75e705a15b400e7ce4f64d0b0f0bcc6c to your computer and use it in GitHub Desktop.
Get node value in a linked list position from tail | JS
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 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