Search⌘ K
AI Features

Find n'th Node from the End of a Linked List

Explore how to find the nth node from the end of a singly linked list using a two-pointer approach. Understand this technique's step-by-step logic, handle edge cases when n exceeds list size, and analyze its linear runtime and constant space efficiency.

Statement

We’re given a singly linked list. Return the nthn^{th} node from the last node. Return null if n is larger than the size of the list.

Example

In the example below, the value of the third to the last node is 3:

g head head a 1 head->a NULL NULL b 1 a->b c 2 b->c d 3 c->d e 4 d->e f 5 e->f f->NULL

Sample input

 ...