# Nth Element from Last Node

## #

QuestionGiven a singly linked list, find the nth node from the last.

Example: A-B-C-D and n = 2, return C

## #

Method 1 - Find Length and IterateTo find the nth element from the end, we have 2 steps:

- Find the length of the linked list
- Use a counter to count from 1 till n as we iterate the linked list

- Time complexity:
`O(n)`

where`n`

is length of linked list

## #

Method 2 - 2 PointersIn this method, we use 2 pointers, `p1`

and `p2`

. We start `p1`

from the head and `p2`

is at `n`

steps ahead of `p1`

.

We increment both pointers till `p2`

reaches the end of the linked list. At this point, `p1`

will be at the nth node from the last node of the linked list.

- Time complexity:
`O(n)`

where`n`

is length of linked list