Java 删除链接列表中的偶数值
我正在练习我的算法,我试图删除链接列表中的值,但失败了。在删除偶数值后尝试打印链表类时,我提供了测试用例,以查看程序是否正常工作。它打印出10和1。11和1应为打印的值Java 删除链接列表中的偶数值,java,linked-list,Java,Linked List,我正在练习我的算法,我试图删除链接列表中的值,但失败了。在删除偶数值后尝试打印链表类时,我提供了测试用例,以查看程序是否正常工作。它打印出10和1。11和1应为打印的值 public class LinkedList { private Node head; public LinkedList() { this.head = null; } public void add(int data) { Node node = new Node(data);
public class LinkedList
{
private Node head;
public LinkedList() {
this.head = null;
}
public void add(int data) {
Node node = new Node(data);
if(head == null) {
node.setNextNode(head);
head = node;
}
else {
Node curr = head;
while(curr.next != null) {
curr = curr.getNextNode();
}
curr.setNextNode(node);
}
}
public void removeEven() {
if (this.head==null) return;
Node prev=null, curr = head;
while(curr != null) {
if(curr.data%2 == 0) {
if(prev == null) {
Node node = curr.getNextNode();
node.setNextNode(prev);
prev = node;
curr = curr.getNextNode().getNextNode();
}
else {
Node node = curr.getNextNode();
prev.setNextNode(node);
curr = curr.getNextNode();
}
// curr = curr.getNextNode();
}
else{
prev = curr;
curr = curr.getNextNode();
}
}
}
public void print() {
if(head == null) return;
Node curr = head;
while(curr != null) {
System.out.println(curr);
curr = curr.getNextNode();
}
}
public static void main(String[] args)
{
LinkedList h = new LinkedList();
h.add(10);
h.add(1);
h.add(11);
h.add(8);
h.removeEven();
h.print();
}
}
class Node {
int data;
Node next;
public Node(int data) {
this(data, null);
}
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
public void setNextNode(Node next) {
this.next = next;
}
public Node getNextNode() {
return this.next;
}
@Override
public String toString() {
return "Data: "+this.data;
}
}
问题是
if(prev==null){
之后的代码。要删除列表的第一个元素,应将head
设置为curr.getNextNode()
。而不是curr.getNextNode().setNextNode(null)
。你应该仔细阅读Java程序的调试。查看和。在你的程序中,给每个方法一个责任:用一个方法删除节点,用另一个方法选择偶数的节点,并用第一个方法删除它们。现在这两个逻辑在一个方法中,这使得调试很困难。提示:问题出在哪里很可能是在您的删除代码中。@ErwinBolwidt是的问题是在我的删除方法中。我有一个简单的问题。为什么要将设置为当前。setNextNode(null)
。这会删除node@EmyItegbe不,它只是停止引用下一个节点。它将被自动垃圾收集。
public class LinkedList
{
private Node head;
public LinkedList() {
this.head = null;
}
public void add(int data) {
Node node = new Node(data);
if(head == null) {
node.setNextNode(head);
head = node;
}
else {
Node curr = head;
while(curr.next != null) {
curr = curr.getNextNode();
}
curr.setNextNode(node);
}
}
public void removeEven() {
if (this.head==null) return;
Node prev=null, curr = head;
while(curr != null) {
if(curr.data%2 == 0) {
if(prev == null) {
Node node = curr.getNextNode();
curr.setNextNode(prev);
prev = node;
curr = prev.getNextNode();
head = prev;
}
else {
Node node = curr.getNextNode();
prev.setNextNode(node);
curr.setNextNode(null);
curr = node;
}
}
else{
prev = curr;
curr = curr.getNextNode();
}
}
}
public void print() {
if(head == null) return;
Node curr = head;
while(curr != null) {
System.out.println(curr);
curr = curr.getNextNode();
}
}
public static void main(String[] args)
{
LinkedList h = new LinkedList();
h.add(10);
h.add(1);
h.add(11);
h.add(8);
h.removeEven();
h.print();
}
}
class Node {
int data;
Node next;
public Node(int data) {
this(data, null);
}
public Node(int data, Node next) {
this.data = data;
this.next = next;
}
public void setNextNode(Node next) {
this.next = next;
}
public Node getNextNode() {
return this.next;
}
@Override
public String toString() {
return "Data: "+this.data;
}
}