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; }
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; }