Delete nth element from headnode using C#

Deleting the nth element from a linked list involves traversing to the target node and adjusting the links to bypass it. When deleting from the head position (n=1), we simply update the head reference. For other positions, we need to find the node before the target and redirect its Next pointer.

Syntax

Following is the basic structure for deleting the nth node −

if (n == 1) {
    head = head.Next;
    return;
}

Node current = head;
for (int i = 1; i 

How It Works

The deletion process involves two main scenarios −

  • Deleting head node (n=1): Simply update the head reference to point to the second node.

  • Deleting other nodes: Traverse to the node before the target, then redirect its Next pointer to skip the target node.

Deleting 3rd Node from Linked List Before: 50 100 150 200 Delete 3rd node After: 50 100 200 Link bypasses deleted node

Example

Here is a complete implementation of deleting the nth element from a linked list −

using System;

class Node {
    public int Data;
    public Node Next;
    
    public Node(int data) {
        Data = data;
        Next = null;
    }
}

class LinkedList {
    public Node head;
    
    public void Push(int data) {
        Node newNode = new Node(data);
        newNode.Next = head;
        head = newNode;
    }
    
    public void DeleteNthNode(int position) {
        if (head == null) return;
        
        if (position == 1) {
            head = head.Next;
            return;
        }
        
        Node current = head;
        for (int i = 1; i  ");
            current = current.Next;
        }
        Console.WriteLine("NULL");
    }
}

class Program {
    public static void Main() {
        LinkedList list = new LinkedList();
        list.Push(200);
        list.Push(150);
        list.Push(100);
        list.Push(50);
        
        Console.WriteLine("Original list:");
        list.Display();
        
        list.DeleteNthNode(3);
        Console.WriteLine("After deleting 3rd element:");
        list.Display();
        
        list.DeleteNthNode(1);
        Console.WriteLine("After deleting head element:");
        list.Display();
    }
}

The output of the above code is −

Original list:
50 -> 100 -> 150 -> 200 -> NULL
After deleting 3rd element:
50 -> 100 -> 200 -> NULL
After deleting head element:
100 -> 200 -> NULL

Key Rules

  • Check bounds: Always verify that the position is valid and the node exists.

  • Handle edge cases: Empty list or attempting to delete beyond the list length.

  • Position counting: Positions typically start from 1 (head node is position 1).

  • Memory management: In languages with manual memory management, remember to free the deleted node.

Conclusion

Deleting the nth element from a linked list requires careful pointer manipulation to maintain list integrity. The key is distinguishing between deleting the head node (simple reassignment) and deleting other nodes (finding the previous node and redirecting its Next pointer).

Updated on: 2026-03-17T07:04:35+05:30

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements