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.
publicstaticintgetNode(SinglyLinkedListNodehead,intpositionFromTail){// Create two pointers, both starting at the headSinglyLinkedListNodefast=head;SinglyLinkedListNodeslow=head;// Move the 'fast' pointer 'positionFromTail' steps aheadfor(inti=0;i<positionFromTail;i++){fast=fast.next;}// Now move both 'fast' and 'slow' one step at a time// When 'fast' reaches the last node, 'slow' will be at the target nodewhile(fast.next!=null){fast=fast.next;slow=slow.next;}// 'slow' is now pointing to the node that is 'positionFromTail' from the endreturnslow.data;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Get Node Value
You are viewing a single comment's thread. Return to all comments →