clear list

This commit is contained in:
shellhub 2019-10-15 22:18:12 +08:00
parent eec7f3e507
commit 14d67ffdf4

View File

@ -106,6 +106,25 @@ public class SinglyLinkedList {
}
}
/**
* clear all nodes in list
*/
public void clear() {
if (size == 0) {
return;
}
Node prev = head.next;
Node cur = prev.next;
while (cur != null) {
prev = null; // clear to let GC do its work
prev = cur;
cur = cur.next;
}
prev = null;
head.next = null;
size = 0;
}
/**
* Checks if the list is empty
*