From ed95f46440453c1203b1ef17709c0ff1321a0f8e Mon Sep 17 00:00:00 2001 From: shinezejian Date: Thu, 21 Nov 2019 20:09:16 +0800 Subject: [PATCH] Update SingleILinkedList.java fixbug --- .../singleLinked/SingleILinkedList.java | 47 +++++++++---------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/com/zejian/structures/LinkedList/singleLinked/SingleILinkedList.java b/src/com/zejian/structures/LinkedList/singleLinked/SingleILinkedList.java index f780483..82e0f6f 100644 --- a/src/com/zejian/structures/LinkedList/singleLinked/SingleILinkedList.java +++ b/src/com/zejian/structures/LinkedList/singleLinked/SingleILinkedList.java @@ -218,36 +218,31 @@ public T remove(int index) { @Override public boolean removeAll(T data) { - boolean isRemove=false; + boolean isRemove = false; - if(this.head!=null&&data!=null){ - - //如果移除的是头结点 - if(data.equals(this.head.data)){ - this.head=this.head.next; - isRemove=true; + if(this.head != null && data != null){ + //如果移除的是头节点 + if(data.equals(this.head.data)){ + this.head = this.head.next; + isRemove = true; + } + + Node front = this.head; + Node pre = front.next; + //查找所有数据相同的节点并删除 + while(pre != null){ + if(data.equals(pre.data)){ + front.next = pre.next; + pre = front.next; + isRemove = true; }else { - - Node front=this.head; - Node pre=front.next; - //查找所有数据相同的结点并删除 - while (pre!=null){ - - if(data.equals(pre.data)){ - //更改指针指向 - front.next=pre.next; - pre =front.next; - isRemove=true; - }else { - front=pre; - pre=pre.next; - } - } + front = pre; + pre = pre.next; } - }else {//data=null || this.head=null - isRemove=false; } - return isRemove; + + } + return isRemove; } /**