# Delete Node without head pointer of the Linked List

“** Delete Node without head pointer of the Linked List**” is a tricky problem based on linked list data structure asked in many technical interview.

Here, We are given a node address of a node to be deleted from the linked list and Our task is to delete that node. The head of the linked list is not given.

**Note**: It is assumed that the node to be deleted is not the last node of the linked list.

The steps required to delete node without head pointer are as follows:

- As we already know that the node to be deleted is not a last node, we will swap the data value of current node with the data value of next node.
- And then delete the next node.

**C++ Program to delete node without head pointer is as follows:**

/* C++ Program to Delete node without head pointer */ #include<bits/stdc++.h> using namespace std; /* Structure of the node of the linked list */ struct node { int data; struct node *next; }; /* Function to insert node at the end of the linked list */ void insert_end(struct node **head, int ele) { /* Creating a new node */ struct node *new_node; new_node = (struct node*)malloc(sizeof(struct node)); new_node->data = ele; new_node->next = NULL; /* Check whether the linked list is empty or not */ /* If the linked list is empty, then new node will become head of the linked list */ if(*head == NULL) { *head = new_node; return; } /* If the linked list is not empty, then traverse the complete linked list and point the pointer of the last node of the linked list to the new node*/ struct node *temp = NULL; temp = (*head); while(temp->next != NULL) temp=temp->next; temp->next = new_node; } /* Function to delete given node of the linked list */ void deleteNode(struct node *Node) { /* As we already know, the node to be deleted is not last node, so swap the data of given node with the data of next node. Then, delete the next node of the linked list */ int temp = Node -> data; Node -> data = Node -> next -> data; Node -> next ->data = temp; Node -> next = Node -> next -> next; delete (Node->next); } /* Function to print the linked list */ void print(struct node *head) { struct node *temp = head; while(temp!=NULL) { cout<<temp->data<<" "; temp=temp->next; } } int main() { struct node *head; head = NULL; /* Insert Some Nodes in the Linked List */ insert_end(&head,4); insert_end(&head,3); insert_end(&head,2); insert_end(&head,1); cout<<"Before deleting 3rd node\n"; print(head); deleteNode(head->next->next); cout<<"\nAfter deleting 3rd node\n"; print(head); }

Before deleting 3rd node 4 3 2 1 After deleting 3rd node 4 3 1OUTPUT:

**Related Posts:**

**Delete Complete Linked List.****Delete Nth Node of the Linked List.****Delete All Occurrences of particular node of the Linked List.****Delete Alternate Nodes of the Linked List.****Delete every ‘N’ Nodes After ‘M’ Nodes of the Linked List.****Remove Duplicate Nodes from an Unsorted Linked List.****Remove Duplicate Nodes from a Sorted Linked List.****Find Union and Intersection of Two Linked List.****Merge Two Sorted Linked List.****Insert a New Node at the Middle of the Linked List.****Insert a New Node at the Sorted Linked List.****Reverse a Linked List.****Reverse a Linked List Using Stack.****Printing Linked List in Reverse Order without actually reversing the Linked List.****Swap Adjacent Elements of the Linked List.****Count All Occurrences of a Particular Node in a Linked List.****Bubble Sort on Linked List.****Detect a Loop in a Linked List.****Find the Length of the Loop present in the Linked List.****Detect and Remove Loop from a Linked List.**