×

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

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.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Difference Between Linear and Non Linear Data Structures

Data Structure A data structure is a data object together with the relationships between the instances and the individual elements that compose an instance. These relationships are defined by the operations...

5 minutes read.

Serialize and Deserialize Binary Trees

In order to save a tree in a file that can later be restored, serialisation is used. The tree's structure must be preserved. Deserialization involves reading a tree from a...

4 minutes read.

Binary Tree to Doubly Linked List

Binary Tree to Doubly Linked List This article will explain how to convert the given binary tree into a Doubly Linked List. The left and right pointers in tree nodes are...

2 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.

Introduction to 2D-Arrays

Two Dimensional Array Technical Definitions An array of arrays is a common definition for a two-dimensional array. A matrix is another name for a two-dimensional array. A matrix looks like a table...

3 minutes read.

What is a Tree in Terms of a Graph?

To know the explanation of trees in terms of graphs, we need first to know what trees and graphs are. So let us first learn about trees and graphs. Trees and...

6 minutes read.

What is a Threaded Binary Tree?

When we consider those binary trees that are interlinked with each other, we do come across the fact that the fields present in there do consist of NULL values that...

3 minutes read.

What is a Spanning Tree in Data Structure

Data structures Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system used to store, manage,...

5 minutes read.

Time Complexity of Selection Sort in Data Structure

What is Time Complexity? The term “Time complexity” can be defined as the number of times executions made of a particular sequence of instructions and not the total amount of time...

3 minutes read.

Introduction to Arrays

What exactly is an array? A group of related data pieces stored in contiguous memory regions is referred to as an array. It is the most basic data structure in which...

5 minutes read.

Primitive Data Structure in C

The data structure is a logical or mathematical model for organizing and structuring the main memory or elements. We can classify the data structures in two ways one is primitive, and...

10 minutes read.

Cocktail Sort

C Program executes cocktail sort. Combo sort is a somewhat straightforward arranging calculation initially planned by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered by Stephen Lacey and Richard Box...

5 minutes read.

Hash Table vs STL Map

Hash table and STL map are extremely valuable information structures in software engineering. Here we will consider the examination between their properties to be well as execution.  To start with, we will...

7 minutes read.

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Priority Queue in Data Structure

Priority Queue A priority queue is a special kind of queue, in priority queue we give some priority to an element and according to this priority an element can be served...

3 minutes read.

Print kth least significant bit number

You have given a number and you have to find out the kth least significant bit of this number. K will be given to you.  The bit will be from...

3 minutes read.

Implementation of Queue

Implementation of queue: We can implement the queue through the array and linked list. An array is the easiest way to implement the queue. When a queue is created with the...

7 minutes read.

Optimal binary search tree using dynamic programming

Implementation // We are creating a presentation where we will present a recursive method of the optimal binary search tree problem.  #include <bits/stdc++.h> using namespace std; //creating a utility function that will help us...

9 minutes read.