You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the Java solution for 8.10, if there is a collision in the bucket where the new element is to be placed, the current code uses collided.remove(c) where collided is the LinkedList object and c is the node in the bucket's list. But calling remove(...) on the LinkedList object itself, and not the iterator, will remove the element by starting at the beginning of the list and traversing until it finds the element. This has 2 problems 1) If the item removed is the second to last element in the list, the loop will never visit the last element (In my opinion the LinkedList iterator should actually throw a ConcurrentModificiationException here, but the check is in next(), and not hasNext() ). 2) This eliminates the benefit of using a LinkedList. The for loop should explicitly use the List's iterator, and then the remove should be done using the remove method of the iterator. This will take O(1) time instead of O(n), where n is the number of items in the bucket.
The text was updated successfully, but these errors were encountered:
primshnick
changed the title
Minor issue with 8.10 Java solution
Bug in 8.10 Java solution
Nov 28, 2014
In the Java solution for 8.10, if there is a collision in the bucket where the new element is to be placed, the current code uses
collided.remove(c)
wherecollided
is theLinkedList
object andc
is the node in the bucket's list. But callingremove(...)
on the LinkedList object itself, and not the iterator, will remove the element by starting at the beginning of the list and traversing until it finds the element. This has 2 problems 1) If the item removed is the second to last element in the list, the loop will never visit the last element (In my opinion the LinkedList iterator should actually throw aConcurrentModificiationException
here, but the check is innext()
, and nothasNext()
). 2) This eliminates the benefit of using a LinkedList. Thefor
loop should explicitly use the List's iterator, and then the remove should be done using theremove
method of the iterator. This will take O(1) time instead of O(n), where n is the number of items in the bucket.The text was updated successfully, but these errors were encountered: