What is the time complexity of deleting the last node in a singly linked list?

Practice Questions

Q1
What is the time complexity of deleting the last node in a singly linked list?
  1. O(1)
  2. O(n)
  3. O(log n)
  4. O(n log n)

Questions & Step-by-Step Solutions

What is the time complexity of deleting the last node in a singly linked list?
  • Step 1: Understand that a singly linked list is a series of nodes where each node points to the next one.
  • Step 2: Identify that to delete the last node, you need to find the node just before it (the second-to-last node).
  • Step 3: Start at the head (first node) of the list and move through each node until you reach the second-to-last node.
  • Step 4: Count how many nodes you have as you traverse the list. This will take time proportional to the number of nodes, which is 'n'.
  • Step 5: Once you reach the second-to-last node, you can change its 'next' pointer to 'null' to remove the last node.
  • Step 6: Conclude that since you had to look at each node to find the second-to-last one, the time complexity for deleting the last node is O(n).
No concepts available.
Soulshift Feedback ×

On a scale of 0–10, how likely are you to recommend The Soulshift Academy?

Not likely Very likely