Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5715 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 1115 |

@bt8816103042 | 739 |

@SherlockHolmes3 | 447 |

@codeantik | 441 |

@SHOAIBVIJAPURE | 430 |

@shalinibhataniya1097 | 408 |

@ShamaKhan1 | 392 |

@neverevergiveup | 381 |

@amrutakashikar2 | 355 |

@mahlawatep | 353 |

@murarry3625 | 352 |

Complete Leaderboard |

Given a linked list consisting of **L** nodes and given a number **N**. The task is to find the **N**^{th} node from the end of the linked list.

**Example 1:**

Input:N = 2 LinkedList: 1->2->3->4->5->6->7->8->9Output:8Explanation:In the first example, there are 9 nodes in linked list and we need to find 2nd node from end. 2nd node from end os 8.

**Example 2:**

Input:N = 5 LinkedList: 10->5->100->5Output:-1Explanation:In the second example, there are 4 nodes in the linked list and we need to find 5th from the end. Since 'n' is more than the number of nodes in the linked list, the output is -1.

**Your Task:**

The task is to complete the function **getNthFromLast**() which takes two **arguments**: **reference **to **head and N** and you need to** return N ^{th} **from the end or -1 in case node doesn't exist..

**Note:**

Try to solve in single traversal.

**Expected Time Complexity: **O(N).

**Expected Auxiliary Space: **O(1).

**Constraints:**

1 <= L <= 10^{3}

1 <= N <= 10^{3}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Nth node from end of linked list

...