Placement Prep

Find Nth Node from End of a Linked List: Two Approaches

Two methods to return the Nth node from the end of a linked list: the length-based two-pass approach and the single-pass two-pointer technique.

By FACE Prep Team 5 min read
linked-list data-structures two-pointers placement-prep algorithms python interview-questions

Two methods return the Nth node from the end of a linked list: count the total length first, or keep two pointers N apart and walk them to the end together.

Both methods run in O(n) time. Interviewers in placement technical rounds generally prefer the two-pointer version because it works in a single pass, but knowing both, and why they are equivalent, is what separates a confident answer from a memorised one.

The problem, illustrated

A linked list is a sequence of nodes where each node holds a value and a pointer to the next. Unlike an array, it has no index; to reach a position you must walk from the head.

Take this list:

1 → 2 → 3 → 4 → 5

Task: return the Nth node from the end, where N = 4.

Counting from the right:

  • 1st from end: value 5
  • 2nd from end: value 4
  • 3rd from end: value 3
  • 4th from end: value 2

The answer is the node with value 2.

A quick check using the position formula: list length is 5, target from the start is (5 - 4 + 1) = 2. The 2nd node from the start holds value 2. Both framings agree.

Method 1: Length-based (two passes)

The straightforward approach: measure the list, then walk to the right spot.

Algorithm

  1. Traverse the full list, counting every node to find the total length L.
  2. Compute the target position from the start: target = L - N + 1.
  3. Traverse again, stopping at that position, and return the node.

Python implementation

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


def nth_from_end_length(head, n):
    # Pass 1: count nodes
    length = 0
    temp = head
    while temp:
        length += 1
        temp = temp.next

    # Validate N
    target = length - n + 1
    if target < 1 or target > length:
        return None  # n exceeds list length

    # Pass 2: reach target position from the start
    current = head
    for _ in range(target - 1):
        current = current.next

    return current

For N = 4 on a list of length 5, target = 5 - 4 + 1 = 2. The second traversal stops after one step and returns the node with value 2.

Complexity for this method:

  • Time: O(n), two traversals
  • Space: O(1), two pointer variables and one counter

Method 2: Two pointers, one pass

The key observation: if fast runs N steps ahead of slow, the moment fast exits the list, slow is exactly at the Nth node from the end.

No length computation required.

Algorithm

  1. Set both fast and slow to head.
  2. Advance fast exactly N steps.
  3. Advance both fast and slow together, one step at a time, until fast reaches None.
  4. Return slow.

Python implementation

def nth_from_end_two_pointers(head, n):
    fast = head
    slow = head

    # Advance fast n steps ahead
    for _ in range(n):
        if fast is None:
            return None  # n exceeds list length
        fast = fast.next

    # Advance both until fast exits the list
    while fast:
        fast = fast.next
        slow = slow.next

    return slow

Trace for N = 4, list length = 5

Step-by-step from both pointers starting at node 1:

  • fast advances 4 steps: lands on node 5 (the last node)
  • Iteration 1 of the while loop: fast moves to None, slow moves from node 1 to node 2
  • fast is None, loop ends
  • slow points to node 2 ✓

The while loop runs exactly once here because fast started one step from the end. If N were 1, the while loop would run 4 times, walking slow all the way to node 5. If N equals the list length, fast reaches None after the advance stage and the while loop never runs, leaving slow at head.

Complexity for this method:

  • Time: O(n), one traversal (fast covers the entire list)
  • Space: O(1), two pointer variables

Time and space complexity

MethodTimeSpace
Length-based (two passes)O(n)O(1)
Two-pointer (one pass)O(n)O(1)

Both methods use constant extra space. Neither allocates an array or any data structure that grows with input size. When the list is very long and N is close to the length, the two-pointer method’s single-pass advantage is real in wall-clock time, even though Big-O classes match.

For a wider look at how constant-space versus linear-space choices play out across different algorithms, see space complexity of algorithms with examples.

Edge cases and interview pitfalls

Interviewers tend to follow up a working solution with one of these cases. Having the answers ready shows you understand the algorithm, not just the happy path.

N is greater than the list length

In the length-based method, target = L - N + 1 goes below 1. Return None.

In the two-pointer method, fast becomes None before completing N steps. The if fast is None: return None guard inside the advance loop catches this before the while loop starts.

N equals the list length (returns head)

In the length-based method, target = L - L + 1 = 1. The second traversal runs zero iterations and returns head directly.

In the two-pointer method, fast completes N steps and lands on None. The while loop body never executes. slow stays at head, which is the correct answer.

Single-node list with N = 1

Length-based: target = 1 - 1 + 1 = 1, returns head (the only node).

Two-pointer: fast moves one step to None. While loop does not run. slow = head.

The remove variation

LeetCode Problem 19 asks you to remove the Nth node from the end, not return it. The two-pointer setup is identical, but you stop slow one node short and rewire slow.next to skip the target. This variation surfaces in placement technical rounds alongside other pointer-manipulation questions at product companies, including those covered in PayPal interview process guides.

The two-pointer pattern also generalises to other list and array problems. Finding symmetric pairs in an array uses a similar two-index scan where both pointers close in from opposite ends.

The two-pointer pattern in larger systems

The O(1) space property of the two-pointer approach means two variables scan a list of any size. The same offset logic reappears in how language models process token sequences: a context window slides over the input, maintaining a fixed gap between where new tokens arrive and where older context drops off. TinkerLLM at ₹299 is the entry point if you want to experiment with those patterns in live LLM code, without committing to a full programme first.

Primary sources

Frequently asked questions

What is the time complexity of finding the Nth node from the end?

Both methods run in O(n) time. The length-based method makes two passes over the list. The two-pointer method makes one pass but fast still traverses the entire list, so the constant differs but the Big-O class is the same.

How does the two-pointer approach work step by step?

Set both fast and slow to head. Move fast exactly N steps ahead. Then advance both pointers one step at a time until fast becomes None. At that point, slow points to the Nth node from the end.

What happens when N is greater than the length of the list?

This is an invalid input. In the length-based method the target position goes below 1, so return None. In the two-pointer method, fast becomes None before completing N steps — the guard inside the advance loop catches this and returns None.

What edge case occurs when N equals the total number of nodes?

When N equals the list length, the Nth node from the end is the head. In the two-pointer method, fast completes N steps and reaches None, so the while loop body never runs and slow stays at head. In the length-based method, target equals 1, so the second pass also returns head.

What is the difference between the length-based and two-pointer methods?

The length-based method traverses the list twice: once to count nodes, once to reach the target. The two-pointer method traverses once by keeping fast N steps ahead of slow. Both use O(n) time and O(1) extra space.

How is this problem typically asked in placement technical rounds?

Placement tests present the problem with a given list and a specific N, asking for the node value. Common variations include deleting the Nth node from the end, or finding the middle node using a similar two-pointer technique where fast moves twice as fast as slow.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF