<jupyter_start><jupyter_text>This notebook was prepared by [<NAME>](http://donnemartin.com). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).  Solution Notebook  Problem: Find the kth to last element of a linked list.* [Constraints](Constraints)* [Test Cases](Test-Cases)* [Algorithm](Algorithm)* [Code](Code)* [Unit Test](Unit-Test)  Constraints* Can we assume this is a non-circular, singly linked list?    * Yes* Can we assume k is a valid integer?    * Yes* If k = 0, does this return the last element?    * Yes* What happens if k is greater than or equal to the length of the linked list?    * Return None* Can you use additional data structures?    * No* Can we assume we already have a linked list class that can be used for this problem?    * Yes  Test Cases* Empty list -> None* k is >= the length of the linked list -> None* One element, k = 0 -> element* General case with many elements, k < length of linked list  Algorithm* Setup two pointers, fast and slow* Give fast a headstart, incrementing it once if k = 1, twice if k = 2, ...* Increment both pointers until fast reaches the end* Return the value of slowComplexity:* Time: O(n)* Space: O(1)  Code<jupyter_code>%run ../linked_list/linked_list.py
class MyLinkedList(LinkedList):

    def kth_to_last_elem(self, k):
        if self.head is None:
            return None
        fast = self.head
        slow = self.head

        # Give fast a headstart, incrementing it
        # once for k = 1, twice for k = 2, etc
        for _ in range(k):
            fast = fast.next
            # If k >= num elements, return None
            if fast is None:
                return None

        # Increment both pointers until fast reaches the end
        while fast.next is not None:
            fast = fast.next
            slow = slow.next
        return slow.data<jupyter_output><empty_output><jupyter_text>Unit Test<jupyter_code>%%writefile test_kth_to_last_elem.py
import unittest


class Test(unittest.TestCase):

    def test_kth_to_last_elem(self):
        print('Test: Empty list')
        linked_list = MyLinkedList(None)
        self.assertEqual(linked_list.kth_to_last_elem(0), None)

        print('Test: k >= len(list)')
        self.assertEqual(linked_list.kth_to_last_elem(100), None)

        print('Test: One element, k = 0')
        head = Node(2)
        linked_list = MyLinkedList(head)
        self.assertEqual(linked_list.kth_to_last_elem(0), 2)

        print('Test: General case')
        linked_list.insert_to_front(1)
        linked_list.insert_to_front(3)
        linked_list.insert_to_front(5)
        linked_list.insert_to_front(7)
        self.assertEqual(linked_list.kth_to_last_elem(2), 3)

        print('Success: test_kth_to_last_elem')


def main():
    test = Test()
    test.test_kth_to_last_elem()


if __name__ == '__main__':
    main()
%run -i test_kth_to_last_elem.py<jupyter_output>Test: Empty list
Test: k >= len(list)
Test: One element, k = 0
Test: General case
Success: test_kth_to_last_elem
{'fraction_non_alphanumeric': 0.09609708101016727, 'fraction_numerical': 0.007215480485405051, 'mean_word_length': 3.0829986613119145, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '322726', 'license': 'Apache-2.0', 'n_tokens_mistral': 969, 'n_tokens_neox': 929, 'n_words': 311, 'path': 'linked_lists/kth_to_last_elem/kth_to_last_elem_solution.ipynb', 'repo_name': 'benkeesey/interactive-coding-challenges', 'size': '3055'}