We want to find an item in a linked list, do something with the item, and then delete it.
from collections import deque
q = deque([(12, 'apples'), (32, 'oranges'), (42, 'pears'), (12, 'peaches')])
john_smith_id = 42
for customer in q:
id, data = customer
if id == john_smith_id:
myfunction(data) # do something with John Smith's data
q.remove(customer)
break
My question is about the line q.remove(customer)
. This should ideally be O(1)
since we've already found the customer in the linked list, but I fear it may be traversing the entire linked list.
What's the correct way to do this?