Implement an algorithm to find the kth to last element of a singly linked list.
T findKthToLast(List<T> list, k) { if (k = 0) return null; int size = 0; Map<Integer, T> elementMap = new HashMap<Integer, T>(); for (T element : list) { size++; elementMap.put(size, element); } return elementMap.get(size - k + 1); }
No comments:
Post a Comment