gpt4 book ai didi

java - 删除链表中的元素

转载 作者:行者123 更新时间:2023-12-02 03:34:14 27 4
gpt4 key购买 nike

如果我有包含一些元素的链表。例如,myLinked={3,6 100,95 ,16, 19 ,7 13 ,44}。我想删除 8 到 20 之间的任何元素。链表将删除 7, 16 ,19,13。有人可以告诉我应该怎么做吗?另外,我可以在不使用排序的情况下执行此操作吗?

这是我的 LinkedList 类:

public class MyList
{
static class Node
{
public Node(double item, Node next)
{
this.item = item;
this.next = next;
}

public double item;
public Node next;
}

Node first;

public MyList()
{
first = null;
}

public boolean isEmpty()
{
return first == null;
}

public void add (double item)
{
Node newfirst = new Node (item, first);
this.first = newfirst;
}
}

最佳答案

    int min = 8; //the min value you want to remove
int max = 20; //the max value you want to remove

for (int i = 0; i < myLinkedList.size(); i++) {
int current = myLinkedList.get(i); //get the current element of your list
if(current > min && current < max){ //check if current is between those numbers
myLinkedList.remove(i);
i--;
}
}

关于java - 删除链表中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37635271/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com