From ccea4b56e35754a7a1a04989bb8413d45f71029f Mon Sep 17 00:00:00 2001 From: shellhub Date: Wed, 9 Oct 2019 11:24:35 +0800 Subject: [PATCH] optimization and fix bug --- DataStructures/Lists/CircleLinkedList.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/DataStructures/Lists/CircleLinkedList.java b/DataStructures/Lists/CircleLinkedList.java index 8863b7349..67235172d 100644 --- a/DataStructures/Lists/CircleLinkedList.java +++ b/DataStructures/Lists/CircleLinkedList.java @@ -44,22 +44,20 @@ public class CircleLinkedList { //catching errors throw new IndexOutOfBoundsException("position cannot be greater than size or negative"); } - Node iterator = head.next; //we need to keep track of the element before the element we want to remove we can see why bellow. Node before = head; for (int i = 1; i <= pos; i++) { - iterator = iterator.next; before = before.next; } - E saved = iterator.value; + Node 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; } } -