EXERCISE · AlgoCasts

From last (nth from end)

Given a linked list, return the element n spaces from the last node in the list. Do not call the 'size' method of the linked list.

2 testsAlgoCastsGPL-3.0exercise 21 of 26

Directions

Directions

Given a linked list, return the element n spaces from the last node in the list. Do not call the 'size' method of the linked list. Assume that n will always be less than the length of the list.

Examples#

JS
 const list = new List(); list.insertLast('a'); list.insertLast('b'); list.insertLast('c'); list.insertLast('d'); fromLast(list, 2).data // 'b'

Try it first#

Copy the stub, make the 2 tests pass on your machine, then open the solution. The reveal is remembered per page in this browser.

JSexercises/fromlast/index.js
function fromLast(list, n) {} module.exports = fromLast;

See also