Write a method for the Linked List class which takes a number, k, as a parameter. Return the node’s value that is k from the end of the linked list.
Extend the linked list with these three different methods:
.kthFromEnd(k)
(returns the node’s value that isk
from the end of the linked list)
Input: a numberk
used as a search value Output: the value of the nodek
places from the end of the linked list
Edge case(s):
- An empty linked list (Throw error)
k
is greater than nodes in the linked list. (Throw error)k
is not a positive integer (Throw error)k
is ‘1’ and linked list has only 1 node (Can handle)
Write tests to prove the following functionality: