Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

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.

Challenge | 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 shouldBeEqualTo 'e'
fromLast(l, 3)?.data shouldBeEqualTo 'b'

Hints

Hint 1 desc