Skip to content

Commit c909d68

Browse files
committed
Updated list length during deletion.
1 parent d82b458 commit c909d68

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/chapter03linkedlists/LinkedList.java

+4
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,16 @@ public synchronized void removeMatched(ListNode node) {
136136
return;
137137
if (node.equals(head)) {
138138
head = head.getNext();
139+
// reduce the length of the list
140+
length-=1;
139141
return;
140142
}
141143
ListNode p = head, q = null;
142144
while((q = p.getNext()) != null) {
143145
if (node.equals(q)) {
144146
p.setNext(q.getNext());
147+
// reduce the length of the list
148+
length-=1;
145149
return;
146150
}
147151
p = q;

0 commit comments

Comments
 (0)