Skip to content

optimization and fix bug #997

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions DataStructures/Lists/CircleLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,20 @@ public E remove(int pos) {
//catching errors
throw new IndexOutOfBoundsException("position cannot be greater than size or negative");
}
Node<E> iterator = head.next;
//we need to keep track of the element before the element we want to remove we can see why bellow.
Node<E> before = head;
for (int i = 1; i <= pos; i++) {
iterator = iterator.next;
before = before.next;
}
E saved = iterator.value;
Node<E> destroy = before.next;
E saved = destroy.value;
// assigning the next reference to the the element following the element we want to remove... the last element will be assigned to the head.
before.next = iterator.next;
before.next = before.next.next;
// scrubbing
iterator.next = null;
iterator.value = null;
destroy = null;
size--;
return saved;

}

}