Solution: Deletion in Singly Linked List (Delete by Value)

This review provides a detailed analysis of how to solve the Deletion in the Singly linked list challenge.

We'll cover the following

Explanation

While traversing through the linked list, there are three cases we can come across:

  • List is empty
  • One element in the list
  • More than one element in the list

If the list is empty, then we have nothing to do; just return the control to calling function. For a single element in the list, we’ll call deleteAtHead(), and this will delete the value at the head.

If there is more than one element in the list, then we simply start traversing from the headNode and keep on checking until that node found and delete it. The following figure illustrates this process:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.