Code Logo

Nth Node From End

Published at25 Jul 2026
Easy 0 views
Like0

Given an array and a number n, return the nth element from the end (1-indexed). This simulates finding the nth node from the end of a linked list.

Example Input & Output

Example 1
Input
[5],1
Output
5
Explanation

Single element

Example 2
Input
[1,2,3],1
Output
3
Explanation

1st from end is last element

Example 3
Input
[10,20,30,40,50],2
Output
40
Explanation

2nd from end is 40

Example 4
Input
[1,2,3,4,5],5
Output
1
Explanation

5th from end is first

Example 5
Input
[10],1
Output
10
Explanation

Single

Algorithm Flow

Recommendation Algorithm Flow for Nth Node From End
Recommendation Algorithm Flow for Nth Node From End

Solution Approach

function solution(arr, n) {
  return arr[arr.length - n];
}

Best Answers

java
class Solution {
    public int solution(int[] nums, int n) {
        return nums[nums.length-n];
    }
}