Skip to content

Commit

Permalink
optimization and fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
realDuYuanChao committed Oct 9, 2019
1 parent 993f30b commit ccea4b5
Showing 1 changed file with 5 additions and 7 deletions.
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;

}

}

0 comments on commit ccea4b5

Please sign in to comment.