尝试编写一个方法,从单个链接列表中删除值的所有实例,但它似乎不起作用。
我试图了解头部是否包含值,但我不确定这是否是正确的方法:
public void remove (int value)
{
if (head.value == value)
{
head = head.next;
count--;
}
IntegerNode temp=head;
while (temp !=null)
{
if (temp.next != null)
{
if (temp.next.value == value)
{
temp.next = temp.next.next;
count--;
}
}
temp=temp.next;
}
}
我的代码有什么问题吗?
答案 0 :(得分:1)
此处使用add
和remove
方法实现链接列表并进行测试。
public class ListDemo {
public static void main(String[] args) {
MyList list = new MyList();
list.addToEnd(1);
list.addToEnd(2);
list.addToEnd(3);
list.removeByValue(2);
list.removeByValue(3);
}
}
class MyList {
private IntegerNode head;
private int count = 0;
public void addToEnd(int value) {
if(head == null) {
head = new IntegerNode(value);
count = 1;
head.next = null;
return;
}
IntegerNode current = head;
while (current.next != null) {
current = current.next;
}
IntegerNode node = new IntegerNode(value);
node.next = null;
count++;
current.next = node;
}
public void removeByValue(int value) {
if (count == 0) {
return;
} else if (count == 1) {
if (head.value == value) {
count = 0;
head = null;
}
} else {
IntegerNode current = this.head;
IntegerNode next = current.next;
while (next != null) {
if (next.value == value) {
if (next.next == null) {
current.next = null;
count--;
return;
} else {
current.next = next.next;
count--;
}
}
next = next.next;
}
}
}
}
class IntegerNode {
IntegerNode(int value) {
this.value = value;
}
IntegerNode next;
int value;
}
答案 1 :(得分:0)
这里有从链接列表中删除的不同方法
public Node removeAtFront()
{
Node returnedNode = null;
if(rootNode !=null)
{
if(rootNode.next !=null)
{
Node pointer = rootNode;
returnedNode = rootNode;
pointer = null;
rootNode = rootNode.next;
}
else
{
Node pointer = rootNode;
returnedNode = rootNode;
pointer = null;
rootNode = rootNode.next;
System.out.println("removing the last node");
}
}else
{
System.out.println("the linkedlist is empty");
}
return returnedNode;
}
public Node removeAtBack()
{
Node returnedNode = null;
if(rootNode != null)
{
//Remove the commented line if you wish to keep 1 node as minimum in the linked list
//if(rootNode.next !=null)
//{
Node pointer = new students();
pointer = rootNode;
while(pointer.next.next !=null)
{
pointer=pointer.next;
}
returnedNode = pointer.next.next;
pointer.next.next = null;
pointer.next = null;
//}
//else
//{
// System.out.println("cant remove the last node because its the root node");
//}
}
else
{
System.out.println("the linkedlist is empty");
}
return returnedNode;
}
public Node removeNode(String name)
{
Node returnedNode = null;
if(rootNode !=null)
{
Node pointer = new students();
Node previous = new students();
pointer = rootNode;
while(pointer !=null)
{
if(pointer.name.equals(name))
{
previous.next = pointer.next;
returnedNode = pointer;
pointer = null;
break;
}
else
{
previous = pointer;
pointer = pointer.next;
}
}
}
else
{
System.out.println("the linkedlist is empty");
}
return returnedNode;
}
public boolean isEmpty() {
return rootNode == null;
}
答案 2 :(得分:0)
您需要跟踪列表中的位置而不是您要去的位置。像这样:
public void remove (int value)
{
IntegerNode current = head;
while (current !=null)
{
if (current.value == value)
{
if (head == current)
{
head = current.next;
}
else
{
head.next = current.next;
}
count--;
}
current=current.next;
}
}