Removing the Nth Node from the End of a Linked List

In this tutorial, we will learn how to remove the nth node from the end of a linked list. This is a common problem in data structures and can be a bit tricky if you’re new to linked lists. But don’t worry! We’ll break it down step by step.

Prerequisites

Before we dive into the solution, make sure you have a basic understanding of the following concepts:

  • Linked Lists: A linked list is a linear data structure where each element is a separate object, called a node. Each node contains data and a reference (or link) to the next node in the sequence.
  • Basic Programming Skills: Familiarity with a programming language such as Python, Java, or C++ will help you follow along with the examples.

Step-by-Step Guide

Let’s go through the steps to remove the nth node from the end of a linked list.

Step 1: Understand the Problem

Given a linked list, we need to remove the node that is nth from the end. For example, if we have a linked list of 5 nodes and we want to remove the 2nd node from the end, we will remove the 4th node from the start.

Step 2: Create a Linked List

First, we need to create a linked list. Here’s a simple representation of a linked list:

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

Step 3: Implement the Removal Logic

To remove the nth node from the end, we can use a two-pointer technique. Here’s how it works:

  1. Initialize two pointers, fast and slow, both pointing to the head of the linked list.
  2. Move the fast pointer n steps ahead.
  3. Then, move both pointers at the same speed until the fast pointer reaches the end of the list.
  4. At this point, the slow pointer will be just before the node we want to remove.
  5. Adjust the pointers to remove the desired node.

Step 4: Code Implementation

Here’s how the complete code looks:

def removeNthFromEnd(head, n):
    dummy = ListNode(0)
    dummy.next = head
    fast = slow = dummy

    for _ in range(n + 1):
        fast = fast.next

    while fast:
        fast = fast.next
        slow = slow.next

    slow.next = slow.next.next
    return dummy.next

Explanation

In the code above:

  • We create a dummy node that points to the head of the list. This helps handle edge cases, such as when we need to remove the head itself.
  • We move the fast pointer n + 1 steps ahead to maintain the gap between the two pointers.
  • Once the fast pointer reaches the end, the slow pointer will be just before the node we want to remove.
  • Finally, we adjust the slow pointer’s next reference to skip the node we want to remove.

Conclusion

Removing the nth node from the end of a linked list can be efficiently done using the two-pointer technique. This method allows us to traverse the list in a single pass, making it both time-efficient and space-efficient.

Now that you have a solid understanding of how to tackle this problem, you can practice implementing it in your preferred programming language. Happy coding!

For more information, check out the following resources:

https://olehslabak.medium.com/leetcode-19-remove-nth-node-from-end-of-list-a41ddcd27b40?source=rss——algorithms-5

Continue reading on Medium »

Source: Original Article