clear list
This commit is contained in:
parent
eec7f3e507
commit
14d67ffdf4
@ -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
|
* Checks if the list is empty
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user