Snippets Collections
int getNthFromLast(Node *head, int n)
{
       // Your code here
       Node* front = head; 
       while(front && n--)front = front->next;
       if(n>0)
        return -1;
       Node* rear = head;
       while(front){
           front = front->next;
           rear = rear->next;
       }
       return rear->data;
}
star

Sat May 27 2023 19:54:44 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/nth-node-from-end-of-linked-list/1

#c++ #linked_list #nth_node_from_end

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension