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;
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter