Skip to content

Commit ed95f46

Browse files
authored
Update SingleILinkedList.java
fixbug
1 parent 61e8971 commit ed95f46

1 file changed

Lines changed: 21 additions & 26 deletions

File tree

src/com/zejian/structures/LinkedList/singleLinked/SingleILinkedList.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -218,36 +218,31 @@ public T remove(int index) {
218218
@Override
219219
public boolean removeAll(T data) {
220220

221-
boolean isRemove=false;
221+
boolean isRemove = false;
222222

223-
if(this.head!=null&&data!=null){
224-
225-
//如果移除的是头结点
226-
if(data.equals(this.head.data)){
227-
this.head=this.head.next;
228-
isRemove=true;
223+
if(this.head != null && data != null){
224+
//如果移除的是头节点
225+
if(data.equals(this.head.data)){
226+
this.head = this.head.next;
227+
isRemove = true;
228+
}
229+
230+
Node<T> front = this.head;
231+
Node<T> pre = front.next;
232+
//查找所有数据相同的节点并删除
233+
while(pre != null){
234+
if(data.equals(pre.data)){
235+
front.next = pre.next;
236+
pre = front.next;
237+
isRemove = true;
229238
}else {
230-
231-
Node<T> front=this.head;
232-
Node<T> pre=front.next;
233-
//查找所有数据相同的结点并删除
234-
while (pre!=null){
235-
236-
if(data.equals(pre.data)){
237-
//更改指针指向
238-
front.next=pre.next;
239-
pre =front.next;
240-
isRemove=true;
241-
}else {
242-
front=pre;
243-
pre=pre.next;
244-
}
245-
}
239+
front = pre;
240+
pre = pre.next;
246241
}
247-
}else {//data=null || this.head=null
248-
isRemove=false;
249242
}
250-
return isRemove;
243+
244+
}
245+
return isRemove;
251246
}
252247

253248
/**

0 commit comments

Comments
 (0)