×

Delete a Node without head pointer from the linked list

Delete a Node without head pointer from the linked list

This article will explain how to delete a node without a head pointer from the linked list. We have given a singly linked list and the node's reference, which needs to be deleted. We do not have any information about the starting address or head pointer. We need to write a function to delete that node from the linked list, and it should not be the first node of the linked list.

Method

It is easy to solve this problem if we have given the head pointer of the linked list. Because for deletion of the node, we must know the previous node pointer, and we can reach to desire node, which needs to be deleted by traversing from the head pointer, but this is not possible in this case.

So, for the deletion of that node, we need to copy the data of the next node to the data field of the current node that we are going to delete. Then, the next node has become the current node, and the current node has become the previous node. Now, we will delete the current node by our traditional delete method.

Algorithm for deleting the node without head pointer from the linked list:

 void delete(struct node * curr)
 {
     struct node * temp = curr;
     curr -> info = curr -> next -> info;
     curr -> next = curr -> next -> next;
 } 

Defination of structure of a node is as follows:

 struct node {
     int info;
     struct Node* next;
 }; 

C program to delete a node without head pointer from the linked list.

 #include<stdio.h>
 #include<stdlib.h>
 struct node
 {
 int info;
 struct node *next;
 };
 struct node *start = NULL;
 // For inserting the elements in the linked list
 void add(int item)
 {
  struct node *t, *p;
  t = (struct node *)malloc( sizeof( struct node ));
 if(start == NULL)
 {
  start = t;
  start -> info = item;
  start -> next = NULL;
  return;
 }
 else
 {
 struct node *p = start;
 while(p -> next != NULL)
 {
  p = p -> next;
 }
  p -> next = t;
  p = p -> next;
  p -> info = item;
  p -> next = NULL;
  }
 }
 // For deleting the node without head pointer from the linked list
 void delete(struct node * curr)
 {
     struct node * temp = curr;
     curr -> info = curr -> next -> info;
     curr -> next = curr -> next -> next;
 }
 // To display the elements of the linked list
 void traverse(struct node * t)
 {
 if(t == NULL)
 {
             printf(" Linked list is empty\n");
                                     }
                                     while(t -> next != NULL)
                                     {
                         printf("%d -> ", t -> info);
                         t = t -> next;
                         }
                         printf("%d\n", t -> info);
 }
 // Driver Function
 int main()
 {
     int i;
     for (i = 2; i < 12; i+=2)
     {
         add(i);
     }
     printf("Before deletion:");
     traverse(start);
     delete(start -> next -> next -> next);
     printf("After deletion:");
     traverse(start);
     return 0;
 } 

Output

Delete a Node

Time complexity: The time complexity of this method is O(1), which is constant time.


Related Topics

Insertion Sort in Data Structures

Insertion Sort in C++ Insertion sort is a sorting algorithm that, in each iteration, installs an unsorted element in its proper position Insertion sort operates in a similar way to how we...

3 minutes read.

Bin Packing Problem (How to minimize the number of used Bins)

You have been given an array. The values of the array represent the size of n different items. You have been also given some bins. You have to store the...

3 minutes read.

Binary search tree traversal in-order pre-order post-order examples

A binary search tree is a type of non-linear tree in which the tree contains at least two nods. It is called binary because of its nature that states bi...

8 minutes read.

Data Structure Infix to Postfix Conversion

Infix to Postfix Conversion The infix expression is easy to read and write by humans. In present time, we use the infix expression in our daily life but the computers are...

4 minutes read.

Minimum Spanning Tree

Before getting to know about the minimum spanning tree, we should first discuss about what is a spanning tree. A spanning tree is basically a sub or minimized graph that...

7 minutes read.

Introduction and Implementation of Bloom Filter

It often happens with many of us that when we create an account on some applications like Github, it shows us that the username already exists. You can add some...

4 minutes read.

Threaded Binary Trees

Introduction Threaded Binary Trees (TBTs) are an enhancement of normal binary trees intended for in-order traversal only. This means that this data structure is developed with the objective of making the...

12 minutes read.

Recaman’s Sequence

Recamán's succession repeat connection in arithmetic and software engineering. Since its components are obviously connected with the past components, they are as often as possible characterized utilizing recursion. It takes its...

4 minutes read.

Queue operations in Data Structure

Queue - Queue is a linear data structure or first in first out data structure means the first element added in the queue will be removed first and the last...

7 minutes read.

Rearrange a linked list into alternate fashion first and the last element

Rearrange a linked list into alternate fashion first and the last element This article will explain how to rearrange the linked list into alternate fashion first and the last element. Here,...

3 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.

What is an AVL Tree in Data Structure?

AVL tree stands for (Adelson, Velskii, & Landis Tree) Data structure Data management is called database management. A data model is a system used to store, manage, and optimize computer resources. Data...

4 minutes read.

Length of longest palindrome in a linked list using O(1) extra space

Length of longest palindrome in a linked list using O(1) extra space In this problem, we need to find the length of the longest palindrome list that is present in given...

2 minutes read.

Find the nth node from the end of a Linked List

Find the nth node from the end of a Linked List In this problem, we have given a singly linked list and a number 'n,' and we need to find the...

3 minutes read.

Complete Binary tree

In this article, we will discuss the complete binary tree. But before start discussing the complete binary tree, we should first see a brief description of a binary tree. What is...

7 minutes read.

Structure and Union Data Structure

The array is used for the same type of data, but if we want to store a mixed type of data in a group, then the array cannot be used. The Structure...

4 minutes read.

What is a Height-Balanced Tree in Data Structure

A height-balanced tree is a type of binary tree. If the absolute difference between the heights of the left and right subtree is less than or equal to 1, then...

6 minutes read.

Finding the Sum of All Paths in a Binary Tree

Implementation // Writing the C++ program to implement the below approach.  #include <bits/stdc++.h> using namespace std; // creating the new tree node structure. struct Tree__nod { int val; Tree__nod *Lft, *Rt; }; // creating a new function that will...

8 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

How to get Better in Data Structures and Algorithms?

Introduction Data structures and algorithms are fundamental computer science concepts that store, organize, and process data efficiently. By understanding different data structures and algorithms and using them effectively, you can become...

19 minutes read.