Skip to content

Commit d82b458

Browse files
committed
List length needs to be updated on remove operation.
1 parent 78dbc99 commit d82b458

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/chapter03linkedlists/LinkedList.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,17 @@ public synchronized ListNode removeFromEnd() {
115115
ListNode p = head, q = null, next = head.getNext();
116116
if (next == null) {
117117
head = null;
118+
// reduce the length of the list
119+
length-=1;
118120
return p;
119121
}
120122
while((next = p.getNext()) != null) {
121123
q = p;
122124
p = next;
123125
}
124126
q.setNext(null);
127+
// reduce the length of the list
128+
length-=1;
125129
return p;
126130
}
127131

0 commit comments

Comments
 (0)