无法删除链接列表中的最后一个节点

时间:2016-11-06 17:53:04

标签: java linked-list singly-linked-list

我尝试从单链表中删除最后一个节点。但我仍然无法在代码中解决此错误。我的deleteFromEnd方法没有删除最后一个节点。调用delete方法后,它仍然显示我要删除的节点。列表的其余部分将被删除,但最后一个节点本身不会被删除。你能告诉我我错过了什么,或者错误在哪里吗?

链表:

package lab5;

public class LinkedList {

    public static void main(String argsp[]) {
        List ob = new List();

        ob.addAtStart("y", 6);
        ob.addAtStart("w", 4);
        ob.addAtStart("z", 3);

        ob.addAtEnd("a", 3);
        ob.addAtEnd("b", 4);
        ob.addAtEnd("c", 5);

        /*
         * ob.display(); System.out.println("Deleted first one");
         * ob.deleteFromStart();
         */
        ob.display();
        System.out.println("Deleted End one");
        ob.deleteFromEnd();
        ob.display();
    }
}

列表:

package lab5;

public class List {

    Node head;

    public List() {
        head = null;
    }

    public List(Node e) {
        head = e;
    }

    Node oldfirst = null;
    Node lasthead = null;

    public void addAtStart(String name, int age) {
        Node newObject = new Node(name, age);
        newObject.next = head;

        if (oldfirst == null) {
            oldfirst = newObject;
        }
        head = newObject;
        lasthead = head;

    }

    public void display() {
        Node store = head;
        while (store != null) {
            store.display();
            store = store.next;
            System.out.println();
        }
    }

    public void addAtEnd(String name, int age) {
        Node atEndValue = new Node(name, age);
        oldfirst.next = atEndValue;
        oldfirst = atEndValue;
    }

    public void deleteFromStart() {
        if (head.next != null) {
            head = head.next;
        }
    }

    public void deleteFromEnd() {
        Node start = head;
        Node prev = null;
        while (head != null) {
            prev = head;
            head = head.next;
        }
        prev.next = null;
        head = prev;
    }

    public Node search(String name) {
        return head;
    }

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

    public int size() {
        return (head.toString()).length();
    }
}

节点

package lab5;

public class Node {

    String name;
    int age;
    Node next;

    public Node() {
        name = "Abc";
        age = 10;
        next = null;
    }

    public Node(String name, int age) {
        this.name = name;
        this.age = age;
        next = null;
    }

    public void display() {
        System.out.println("Name: " + name + " Age: " + age);
    }
}

3 个答案:

答案 0 :(得分:1)

您正在修改列表的头指针,这是错误的。以下方法对我有用。

   public void deleteFromEnd() {
        Node start = head;
        Node prev = null;

        if(start == null || start.next == null)
        {
            head = null;
            return;
        }
        while (start.next != null) {
            prev = start;
            start = start.next;
        }
        prev.next = null;
    }

在分析了您的代码后,我发现其他一些问题。您需要更新addAtStartaddAtEnd方法。

Node lasthead = null;

public void addAtStart(String name, int age) {
    Node newObject = new Node(name, age);
    newObject.next = head;
    if(head == null)
        lasthead = newObject;
    else if(head.next == null)
        lasthead = head;

    head = newObject;

}


public void addAtEnd(String name, int age) {
    Node atEndValue = new Node(name, age);
    lasthead.next = atEndValue;
    lasthead = atEndValue;
}

原因是,假设我从列表末尾删除了一个节点。我无法在列表末尾添加元素。

答案 1 :(得分:1)

当您从单链表的末尾删除时,您必须执行以下操作:

  1. 遍历列表,并创建一个变量以引用列表中的倒数第二个元素。

  2. 将倒数第二个节点之后的节点设置为空

  3. 在遍历链表时,不应更改head的值,因为这会有效删除整个列表。您已经覆盖了head变量,因此无法找到回到开头的方法。相反,使用初始化为head的临时变量进行迭代。

    最后,请记住考虑列表只有1个元素或已经为空的边缘情况:

    public void deleteFromEnd() {
        Node current = head;
        Node previous = null;
    
        while (current != null && current.next != null) {
            previous = current;
            current = current.next;
        }
    
        if (current == head) {
            head = null;
        }
    
        if (previous != null) {
            previous.next = null;
        }
    }
    

答案 2 :(得分:0)

不要更改链接列表的头部,否则您将松开列表。 尝试修改您的功能:

public void deleteFromEnd() {
        Node start = head;
        Node prev = null;
        if(start == null){
           return;
        }
        if (start.next == null){
           head = null;
           return;
        }
        while (start.next != null) {
            prev = start;
            start = start.next;
        }
        prev.next = null;
    }