From 6665ab262c91d59af1b3e2ce9b731772a8f39c41 Mon Sep 17 00:00:00 2001 From: Sahil Parekh Date: Wed, 29 Jun 2022 09:02:40 -0300 Subject: [PATCH] Add a check of the existance of a next node (#3051) * Fix #2976 Co-authored-by: Sahil Prafulkumar Parekh Co-authored-by: Yang Libin --- .../thealgorithms/datastructures/hashmap/hashing/HashMap.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMap.java b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMap.java index 3225c437d..b583a71eb 100644 --- a/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMap.java +++ b/src/main/java/com/thealgorithms/datastructures/hashmap/hashing/HashMap.java @@ -89,7 +89,9 @@ public class HashMap { public void delete(int key) { if (!isEmpty()) { if (first.getKey() == key) { - first = null; + Node next = first.next; + first.next = null; // help GC + first = next; } else { delete(first, key); }