Java 在链表中删除给定键的节点
我已经编写了代码,可以在给定键的情况下从链表中删除节点。但是,当我尝试删除这里的第一个节点,然后遍历我的列表时,它仍然显示以前存在的第一个节点。有人能告诉我我做错了什么吗?我的整个代码以类名开始Java 在链表中删除给定键的节点,java,Java,我已经编写了代码,可以在给定键的情况下从链表中删除节点。但是,当我尝试删除这里的第一个节点,然后遍历我的列表时,它仍然显示以前存在的第一个节点。有人能告诉我我做错了什么吗?我的整个代码以类名开始 public class LinkedList { //removing Node nested class public void buildList1() { head=new Node(1); head.next=new Node(3);
public class LinkedList {
//removing Node nested class
public void buildList1() {
head=new Node(1);
head.next=new Node(3);
head.next.next=new Node(5);
head.next.next.next=new Node(7);
}
public boolean removeNode(Node head,int x) {
//1 3 5 7---to delete 5
Node q=head;//q
// Node p=head.next;//p
Node prev=null;
if(q!=null && q.data==x) {
head=q.next;
//q=null;
System.out.println("next to head" + head.data);
return true;
}
while(q!=null && q.data!=x) {
prev=q;
q=q.next;
}
if(q==null)
return false;
prev.next=q.next;
return true;
}
public void printList()
{
Node tnode = head;
while (tnode != null)
{
System.out.print(tnode.data+" ");
tnode = tnode.next;
}
}
public static void main(String args[]) {
LinkedList list=new LinkedList();
list.buildList1();
list.printList();
System.out.println(list.removeNode(list.head, 1));
list.printList();
}
}
LinkedList有一个方法库,其中一个是remove(int-index)。如图所示使用它。如果你的
头部节点不是你的第一个节点,而你的第一个真正节点是你(固定的头部的下一个,你的生活会更简单
这样,当删除第一个(实际)节点时,您就不需要特殊的代码来处理这种情况。将head添加为实例变量,并从removeNode函数中删除该参数。您应该能够使用this
关键字在方法中引用此变量
类似这样(未经测试,但希望您了解):
@JDD有一个很好的答案,但我会更容易地使用removeNode
方法
public boolean removeNode(int x) {
tempNode = this.head;
prevNode = null;
if (this.head != null && this.head.data == x) {
this.head = this.head.next;
return true;
}
while (tempNode != null) {
if (tempNode.data == x) {
prevNode.next = tempNode.next;
return true;
}
prevNode = tempNode;
tempNode = tempNode.next;
}
return false;
}
尝试直接使用您创建的列表。可能有两种解决方案。
第一:使用创建的列表
if (q != null && q.data == x) {
this.head = q.next;//use this to refer the list you created
// q=null;
System.out.println("next to head" + head.data);
return true;
}
第二:传递列表本身
public boolean removeNode(LinkedList list, int x) {
// 1 3 5 7---to delete 5
Node q = list.head;// q
}
//Call it like this
System.out.println(list.removeNode(list, 1));
旁白:如果(q.data==x&&q!=null)
这里的条件应该被翻转。你能提供一套更完整的代码吗?你能发布你整个LinkedList类的样子吗?您的removeNode
方法不应将head节点作为参数。head节点应该是LinkedList类的一个实例变量。您的LinkedList
类应该跟踪自己的head
,而不是将其作为参数(对其任何方法)——因此它应该以public class LinkedList{private node head;…}
开头,没有buildList1()或printList()这样的东西在内置类中。我认为这是一个学校项目或类似性质的自定义类。在这种情况下,我建议查看unlink(Node x)源代码。并根据你的情况处理它。这显然是一个个人/学校项目。使用内置库无法达到此目的。应将if(this.head.date==x)
逻辑置于循环之前。。。没有理由运行检查每个循环,因为它没有改变这是真的,但是如果(this.head!=null&&this.head.data==x)
它必须是双条件,否则如果列表为空,它将抛出NullPointerException
if (q != null && q.data == x) {
this.head = q.next;//use this to refer the list you created
// q=null;
System.out.println("next to head" + head.data);
return true;
}
public boolean removeNode(LinkedList list, int x) {
// 1 3 5 7---to delete 5
Node q = list.head;// q
}
//Call it like this
System.out.println(list.removeNode(list, 1));