Skip to content

Commit ccea4b5

Browse files
optimization and fix bug
1 parent 993f30b commit ccea4b5

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

DataStructures/Lists/CircleLinkedList.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,20 @@ public E remove(int pos) {
4444
//catching errors
4545
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
4646
}
47-
Node<E> iterator = head.next;
4847
//we need to keep track of the element before the element we want to remove we can see why bellow.
4948
Node<E> before = head;
5049
for (int i = 1; i <= pos; i++) {
51-
iterator = iterator.next;
5250
before = before.next;
5351
}
54-
E saved = iterator.value;
52+
Node<E> destroy = before.next;
53+
E saved = destroy.value;
5554
// assigning the next reference to the the element following the element we want to remove... the last element will be assigned to the head.
56-
before.next = iterator.next;
55+
before.next = before.next.next;
5756
// scrubbing
58-
iterator.next = null;
59-
iterator.value = null;
57+
destroy = null;
58+
size--;
6059
return saved;
6160

6261
}
6362

6463
}
65-

0 commit comments

Comments
 (0)