# Two algorithms to find the nth to last element of a singly linked list

By | November 11, 2013

This is one of frequently asked questions on the programming interview. There are two kinds of solution: iteration-based and recursion-based.

### Data structure

We first define a toy class for the single linked list.

```class LinkedListNode {

int            data;

this.data = data;
this.next = next;
}
}
```

### Iteration-based algorithm

The iteration-based solution is quite tricky but I really like it. In Cracking the Coding Interview (4ed), the solution uses two pointers p1 and p2. Her algorithm first increaments p2 by (n-1) times, and then moves both p1 and p2 until p2.next reaches the tail of the list. As the result, p1 points to the nth to last element. Because of the copyright, I won’t reproduce her code in this post. If you are interested, please see page 106 for the solution.

### Recursion-based algorithm

The recursion version is straightforward too. We first recursively call a function until we reach the tail of the list. Then we start to count from 0 and increment the counter when a called function is returned. Essentially, it counts the number of popped function calls on the call stack. When the counter equals to n, we record the element, which is the nth to the last.

An easier way to count the number of times we have returned to a recursive call is using a global variable. My solution attempted to avoid it by creating a class Record which stores the returned element (null if not) and the counter.

```class Record {

Integer        i;

this.node = node;
this.i = i;
}
}
```

Then I used a helper function for the recursive purpose.

```private static Record nthToLastRurHelper(
int n) {
// tail
return new Record(null, 0);
}
record.i++;
if (record.i == n) {
return record;
} else {
return record;
}
}
```

To find the nth to last element, call

```static LinkedListNode nthToLastRur(LinkedListNode head, int n) {
}
```

### Test cases

Attached is a snippet code for the testing. The code creates a 10-element linked list, then tests two cases. Both cases should return true.

```// create a list with 10 elements
for(int i=10; i>0; i--) {
}

// return true
// return true
```

## 3 thoughts on “Two algorithms to find the nth to last element of a singly linked list”

1. Antonio Ruballos

Is there any reason why you wouldn’t want to just iterate through the linked list once to get the size, subtract n from the count to get the nth value and loop over nth through the linked list and return the last node it stops at?

In other words:

public static Node getKthToLast(Node head, int k){

}

2. Antonio Ruballos

public static Node getKthToLast(Node head, int k){
Node iter;
int count = 1;
return null;
}
else{
while(iter.next != null){
count++;
iter = iter.next;
}
}

int kth = count – k;
1. Yifan Peng Post author