Deletion in Singly Linked List in C++

What is deletion in singly linked list in C++?

Deletion in singly linked in C++ can be simplified as deleting a node from an already constructed linked list. We can perform three different types of deletion operations on a singly linked-

  • Deleting the beginning
  • Deletion from a specific position
  • Deletion from end
delete

Types of deletion in singly linked list

There can be two different approaches for deletion –

We will look at both of them in this page and understand how to code them.

Singly Linked List Deletion in C++

Deletion in a Singly Linked List for a Value

Let’s have a look at the program

Prime Course Trailer

Related Banners

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

Time and space complexity table:

OperationFunction-basedClass-basedSpace Complexity
Insert at BeginningO(1)O(1)O(1)
Delete Node (Best Case)O(1)O(1)O(1)
Delete Node (Worst Case)O(n)O(n)O(1)
Display Linked ListO(n)O(n)O(1)

Deletion in a Singly Linked List in C++ (For a Position)

Deletion can be performed at the following positions –

Below we will write a program that will handle all the above cases in a single function, however, if you wish to write individual functions for each you can check the pages hyperlinked above.

Time and space complexity table:

OperationTime ComplexitySpace Complexity
Insert at BeginningO(1)O(1)
Delete at PositionO(n) — due to traversal and size calculationO(1)
Calculate SizeO(n) — traverses entire listO(1)
Display Linked ListO(n)O(1)

To wrap it up:

It covers various deletion scenarios, including removing nodes from the beginning, middle, and end of the list. The guide emphasizes the importance of pointer manipulation to maintain the integrity of the list structure during deletion operations.

By understanding these deletion techniques, developers can efficiently manage dynamic data structures in their applications. The article also highlights common challenges and best practices to ensure robust and error free implementation of deletion operations in singly linked lists.

Linked List in C++ meme 2

FAQs

To delete the first node, move the head pointer to the second node and free the memory of the original head to avoid memory leaks. This operation has O(1) time complexity.

Traverse the list to the node just before the target position, adjust its next pointer to skip the node, and then delete the target node to free memory.

Traverse the list to reach the second-last node, set its next pointer to NULL, and delete the last node to remove it safely.

Attempting to delete in an empty list does nothing but should be checked to avoid segmentation faults, as the head pointer is NULL.

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
    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

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