Delete N-th Node from End of a Linked List

In the previous article, we saw how to delete the N-th node of a linked list from the beginning of the list. Here we’ll see how to delete N-th node from the end. N starts with 0. Deleting 0-th node from end means deleting the last node.

Logic to Delete the N-th Node from End of a Linked List

  1. If head points to NULL, then return. The list is empty.
  2. Point 2 pointers, prev and end to head.
  3. Move end pointer to (N+1)-th position.
  4. Move prev and end pointers until end becomes NULL.
  5. If end becomes NULL before (N+1)-th iteration, return. Enough nodes do not exist.
  6. Delete the next node of prev. and point prev where the deleted node was pointing.

The Program

/*File: test.c*/

#include <stdio.h>
#include <stdlib.h>

struct node{
    int val;
    struct node *next;
};

/*Delete the n-th node of a linked list from end.*/
void delete_nth_node_from_end(struct node **head, int n) {
  struct node *prev = NULL, *end = NULL;
  struct node *del_node = NULL;
  int i = 0;
  /*Linked list does not exist or the list is empty*/
  if (head == NULL || *head == NULL) return;
  
  /*Storing the head to a temporary variable*/
  prev = *head;
  end = *head;
  
  /*Placing the end pointer to (N+1)-th position...*/
  for (i = 0; i <= n+1; i++) {
    if (end == NULL) break;
    end = end->next;
  }
  
  /*Enough nodes not present.*/
  if (i < n+1) return;

  /*Special case: we have to delete the first node.*/
  if (i == n+1 && end == NULL) {
    *head = (*head)->next;
    free(prev);
    return;
  }
  
  /*Move start and end until end becomes NULL.*/
  while (end) {
    end = end->next;
    prev = prev->next;
  }
  
  /*Node to be deleted.*/
  del_node = prev->next;
  
  /*De-link the n-th node*/
  prev->next = prev->next->next;
  
  free(del_node);
}

/*Print the linked list*/
void print_list(struct node *head) {
    
    printf("H->");

    while(head)
    {
        printf("%d->", head->val);
        head = head->next;
    }

    printf("|||\n");
}

/*Insert a node at the end of the list*/
void insert_end(struct node **head, int value) {
    
    struct node* new_node = NULL;
    struct node* tmp = *head;

    /*Allocating memory for the new node...*/
    new_node = (struct node *)malloc(sizeof(struct node));

    if (new_node == NULL)
    {
        printf("Failed to insert element. Out of memory");
    }

    new_node->val = value;
    new_node->next = NULL;

    /*If the list is empty, the new node becomes the first node.*/
    if (*head == NULL) {
      *head = new_node;
      return;
    }
    
    /*Reaching to the last node...*/
    while(tmp->next) tmp = tmp->next;
    
    tmp->next = new_node;
}

void main()
{
    int count = 0, i, val, n;
    struct node* head = NULL;

    printf("Enter number of elements: ");
    scanf("%d", &count);

    for (i = 0; i < count; i++)
    {
        printf("Enter %d-th element: ", i);
        scanf("%d", &val);
        insert_end(&head, val);
    }

    printf("Initial Linked List: ");
    print_list(head);
    
    printf("Enter a position: ");
    scanf("%d", &n);
    
    delete_nth_node_from_end(&head, n);
    
    printf("Linked List after deleting %d-th node from end: ", n);
    print_list(head);
}

This program first takes input of a linked list and a position (N). It prints the list before and after deleting the N-th node from end. The delete_nth_node_from_end() function is called to delete the N-th node from end. Here is the output of the program.

Author: Srikanta

I write here to help the readers learn and understand computer programing, algorithms, networking, OS concepts etc. in a simple way. I have 20 years of working experience in computer networking and industrial automation.


If you also want to contribute, click here.

Leave a Reply

Your email address will not be published. Required fields are marked *

0
0
0
0
1
0