java单链表删除节点,Java的:从简单的链表中删除节点

I have two lists ItemsList , ilist . If the nodes of ilist contain the same values as the nodes of ItemsList i have to delete them from ItemsList however i get that my list is empty each time i use the remove function from the code below :

public void remove(ItemsList ilist) {

if (empty()) {

System.out.println("The list is empty.");

} else {

this.bubblesort();

ilist.bubblesort();

ItemNode a = this.first;

ItemNode b = ilist.first;

for(a=first;a!=null;a=a.next) {

for(b=first;b!=null;b=b.next) {

if(a.item==b.item) {

this.deleteNode(a.item);

}

}

}

}

}

private void deleteNode(int data) {

ItemNode prev = null;

for(ItemNode trace = first; trace != null; trace = trace.next) {

if(trace.item == data) {

if (prev == null) {

first = trace.next;

} else {

prev.next = trace.next;

}

}

else {

prev = trace;

}

}

}

Lets say i have ItemsList : [0,1,2,3,4] and ilist : [0,1] which means 0 ,1 will be deleted from ItemsList but when i display the ItemsList it says that it's empty . I cannot use arrays , arraylists or other java libraries for the specific problem . Thank you for your time .

解决方案

What I would do:

public void remove(ItemsList iList) {

if (iList.isEmpty()) {

System.out.println("The list is empty.");

} else {

ItemNode prev = null;

ItemNode a = this.first;

while (a != null) {

for (ItemNode b = iList.first; b != null; b = b.next) {

if (a.item == b.item) prev.next = a.next;

}

a = a.next;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值