Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7ebd229

Browse files
Update List.java
1 parent 0986c3d commit 7ebd229

File tree

1 file changed

+22
-22
lines changed
  • algorithms/src/main/java/ivanmarkovic/algorithms/linkedlists/circulardoublylinkedlist

1 file changed

+22
-22
lines changed

‎algorithms/src/main/java/ivanmarkovic/algorithms/linkedlists/circulardoublylinkedlist/List.java

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,32 +97,32 @@ public Integer deleteFromTail() {
9797
return retNode.payload;
9898
}
9999
}
100-
101-
public Integer deleteNodesWithValue(int value) {
100+
101+
public void deleteNodesWithValue(int value) {
102102
if(this.isEmpty())
103103
throw new NoSuchElementException();
104-
else {
105-
Integer retValue = null;
106-
Node current = this.head;
107-
while (current.next != this.head) {
108-
if(current.next.payload == value) {
109-
retValue = current.next.payload;
110-
current.next = current.next.next;
111-
current.next.prev = current;
112-
}
113-
else
114-
current = current.next;
115-
}
116-
this.tail = current;
117-
this.tail.next = this.head;
118-
this.head.prev = this.tail;
104+
if(this.head == this.tail) {
119105
if(this.head.payload == value)
120-
retValue = this.deleteFromHead();
121-
if(retValue == null)
122-
throw new NoSuchElementException();
123-
else
124-
return retValue;
106+
this.deleteFromHead();
107+
return;
108+
}
109+
110+
Node node = this.head;
111+
while (true) {
112+
if (node.next.payload == value) {
113+
boolean toBreak = node.next == this.tail;
114+
node.next = node.next.next;
115+
node.next.prev = node;
116+
if(toBreak)
117+
break;
118+
} else {
119+
node = node.next;
120+
}
125121
}
122+
this.tail = node;
123+
if (this.head.payload == value)
124+
this.deleteFromHead();
125+
126126
}
127127

128128
public void deleteOnIndex(int index) {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /