Learn more about Russian war crimes in Ukraine.

How to find kth last node of a linked list

Question 2.2 of Cracking the Coding Interview:

Implement an algorithm to find the kth to last element of a singly linked list.

There are some boring ways to answer this question, but at least one exciting way.

Boring answer 1: if we know the length l of the list in advance, we can just skip over l - 1 - k nodes.

Boring answer 2: if we don’t know the length of the list in advance, we can count all the nodes first to find its length, then skip over l - 1 - k nodes.

The more exciting answer uses a trick called the “runner”. We set up two pointers into the list, k nodes apart from each other, starting at the start of the list. Then we iterate until the leading pointer hits the end. Then the trailing pointer is k nodes from the end!

Here’s an implementation in C:

#include <stdlib.h>
#include <assert.h>

typedef struct Node {
  int val;
  struct Node * next;
} Node;

Node* kth_last_element(int k, Node* list) {
  Node* trailing = list;
  Node* leading = list;
  for (int i = 0; i <= k; i++) {
    if (leading == NULL) return NULL;
    leading = leading->next;
  while (leading != NULL) {
    leading = leading->next;
    trailing = trailing->next;
  return trailing;

Node* mk_node(int val, Node* next) {
  Node* node = malloc(sizeof(Node));
  node->val = val;
  node->next = next;
  return node;

Node* mk_test_list(int length) {
  if (length <= 0) return NULL;
  return mk_node(length-1, mk_test_list(length-1));

int main(int argc, char** argv) {
  // Indexing empty list should always return NULL
  assert(kth_last_element(0, NULL) == NULL);
  assert(kth_last_element(3, NULL) == NULL);

  // List nodes contain their indexes: 9,8,7,6,5,4,3,2,1,0
  Node* test_list = mk_test_list(10);

  // 0th element from right contains 0, etc
  assert(kth_last_element(0, test_list)->val == 0);
  assert(kth_last_element(5, test_list)->val == 5);

  // Out-of-bounds should return NULL
  assert(kth_last_element(15, test_list) == NULL);

  return 0;

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

More by Jim

Tagged #ctci, #programming, #c. All content copyright James Fisher 2020. This post is not associated with my employer. Found an error? Edit this page.