C Program for deletion at the nth node of the Singly Linked List

Delete a Linked List Node at a given position in C

Deleting any node from the middle is easy, in this blog we will look at ways to delete the nth node of singly linked list, Lets see deletion at the nth node of the Singly Linked List in C .

deletion at the nth node of the Singly Linked List

Deletion at Specific Position

Method for Linked List Delete from Specific node in C

  • Accept the position from the user to delete
  • If it is the first node to delete, change the head to the next node and free the first node memory.
  • While traversing to the nth node, always store the previous (n-1)th node
  • Assign next of (n-1)th node to nth node’s next i.e. (n+1)th node
  • Free the memory for nth node.
deletion at the nth node of the Singly Linked List

C Program for deletion at the nth node of the Singly Linked List

Program to delete the nth node of a Linked list is given below –
Run
#include<stdio.h>
#include<stdlib.h>

struct Node
{
  int data;
  struct Node *next;
};

// required for deletePosition
int getCurrSize (struct Node *node)
{
  int size = 0;

  while (node != NULL)
    {
      node = node->next;
      size++;
    }
  return size;
}

// to delete nth node
void deletePosition (struct Node **head, int n)
{
  struct Node *temp = *head;
  struct Node *previous;

  //if the head node itself needs to be deleted
  int size = getCurrSize (*head);

  // not valid
  if (n < 1 || n > size)
    {
      printf ("Enter valid position\n");
      return;
    }

  // delete the first node
  if (n == 1)
    {
      // move head to next node
      *head = (*head)->next;
      printf ("Deleted: %d\n", temp->data);
      free (temp);
      return;
    }

  // traverse to the nth node
  while (--n)
    {
      // store previous link node as we need to change its next val
      previous = temp;
      temp = temp->next;
    }
  // change previous node's next node to nth node's next node
  previous->next = temp->next;
  printf ("Deleted: %d\n", temp->data);

  // delete this nth node
  free (temp);
}

void display (struct Node *node)
{
  printf ("LinkedList : ");
  // as linked list will end when Node is Null
  while (node != NULL)
    {
      printf ("%d ", node->data);
      node = node->next;
    }
  printf ("\n\n");
}

int main ()
{
  //creating 4 pointers of type struct Node
  //So these can point to address of struct type variable
  struct Node *head = NULL;
  struct Node *node2 = NULL;
  struct Node *node3 = NULL;
  struct Node *node4 = NULL;
  struct Node *node5 = NULL;

  // allocate 3 nodes in the heap 
  head = (struct Node *) malloc (sizeof (struct Node));
  node2 = (struct Node *) malloc (sizeof (struct Node));
  node3 = (struct Node *) malloc (sizeof (struct Node));
  node4 = (struct Node *) malloc (sizeof (struct Node));
  node5 = (struct Node *) malloc (sizeof (struct Node));


  head->data = 25;		// data set for head node 
  head->next = node2;		// next pointer assigned to address of node2 

  node2->data = 20;
  node2->next = node3;

  node3->data = 15;
  node3->next = node4;

  node4->data = 10;
  node4->next = node5;

  node5->data = 5;
  node5->next = NULL;

  display (head);

  // delete 2nd node
  deletePosition (&head, 2);
  display (head);

  // delete 1st node
  deletePosition (&head, 1);
  display (head);

  deletePosition (&head, 3);
  deletePosition (&head, 1);
  deletePosition (&head, 1);
  display (head);		// Linked List becomes empty now

  // invalid, position to delete > size
  // empty linked list size : 0
  deletePosition (&head, 1);

  return 0;
}

Output

LinkedList : 25 20 15 10 5

Deleted: 20
LinkedList : 25 15 10 5

Deleted: 25
LinkedList : 15 10 5

Deleted: 5
Deleted: 15
Deleted: 10
LinkedList :

Enter valid position

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java