We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Get Node Value
Get Node Value
Sort by
recency
|
970 Discussions
|
Please Login in order to post a comment
function getNode(head, positionFromTail) {
const dict = {} let i=0 while(head){ dict[i] = head.data; i++ head=head.next } i=i-1; return dict[i-positionFromTail]
}
I see a lot of solutions posted with double loops and/or a temporary stack of values. It's quite easy to save on repeats and memory with a single pass through the list and keeping track of 2 pointers. Give the one "searching for the end" a "head start" of the amount of provided positions and when you've reached the number, update the pointer to the node with the data.
This is Python Code using stack.
This is my solution
let arr = []