Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (28 sloc) 850 Bytes

Take n-th element from last

Nice to solve before

Instructions

Given a linked list, return the element n positions from the last node in the list. Assume that endIndex will always be less than the length of the list.

Puzzle | Solution

Limitations

Do not use a counter variable, do not retrieve the size of the list, and only iterate through the *whole- list one time.

Examples

val l = LinkedList<Char>()
l.insertLast('a')
l.insertLast('b')
l.insertLast('c')
l.insertLast('d')
l.insertLast('e')

fromLast(l, 0)?.data shouldEqual 'e'
fromLast(l, 3)?.data shouldEqual 'b'

Hints

Hint 1 desc
You can’t perform that action at this time.