使用 Java 删除具有给定值的所有节点



我需要删除所有具有给定值的节点。但是,我的代码删除了它们,但我得到的最终结果是,如果值在头部,它不会删除。如何解决此问题?

public LinkedListNode remove(LinkedListNode head, int value)
{
    if( head == null)
        return head;
    LinkedListNode current = head;
    LinkedListNode trailcurrent = null;
    while(current != null)
    {
        if(current.value == value)
        {
            if(current == head)
            {
                head = head.next;
                current = head;
            }
            else{
                trailcurrent.next = current.next;
                current = trailcurrent.next;
            }
        }
        else
        {
            trailcurrent = current;
            current = current.next;
        }
    }
    return head;
}

您显示的代码是正确的。我添加了一些内容以获得一个完整的示例:

public class LinkedListNode {
    public LinkedListNode next;
    public int value;
}

public class LinkedListTest {
    public static LinkedListNode remove(LinkedListNode head, int value) {
        if (head == null) {
            return head;
        }
        LinkedListNode current = head;
        LinkedListNode trailcurrent = null;
        while (current != null) {
            if (current.value == value) {
                if (current == head) {
                    head = head.next;
                    current = head;
                } else {
                    trailcurrent.next = current.next;
                    current = trailcurrent.next;
                }
            } else {
                trailcurrent = current;
                current = current.next;
            }
        }
        return head;
    }
    public static LinkedListNode createLinkedList(int... values) {
        LinkedListNode result = null;
        for (int i = values.length - 1; i >= 0; i--) {
            LinkedListNode n = new LinkedListNode();
            n.value = values[i];
            n.next = result;
            result = n;
        }
        return result;
    }
    public static void printList(LinkedListNode head) {
        while (head != null) {
            System.out.print(head.value+" ");
            head = head.next;
        }
        System.out.println();
    }
    public static void main(String... args) {
        LinkedListNode head = createLinkedList(3,5,3,2);
        printList(head);
        head = remove(head, 3);
        printList(head);
        printList(remove(head, 5));
    }
}

它打印

3 5 3 2
5 2
2

因此,问题必须出在您显示的代码之外的某个地方。

我尝试在我的系统上运行代码,但进行了一些更改。该程序运行良好:)但我发现了一个见解,我想你会觉得很有趣。无论如何,代码如下所示:

ListNode.java

public class ListNode<T>{
    public T data;
    public ListNode<T> next;
    public int nos;
}

LinkedListPer.java

public class LinkedListPer {
    //ListNode<Integer> list = new ListNode<Integer>();
    ListNode<Integer> add(ListNode<Integer> head, int x){
        ListNode<Integer> p = head;
        if(head==null){
            head = new ListNode<Integer>();
            head.data = x;
        }else{
            while(p.next!=null){
                p=p.next;
            }
            p.next = new ListNode<Integer>();
            p.next.data = x;
        }
        return head;
    }
    
    void display(ListNode<Integer> head){
        ListNode<Integer> p = head;
        while(p.next!=null){
            System.out.print(p.data+"->");
            p = p.next;
        }
        System.out.println("NULL");
    }
    
    ListNode<Integer> delete(ListNode<Integer> head, int val){
        if(head==null){
            return head;
        }
        
        ListNode<Integer> prev = head;
        ListNode<Integer> curr = head;
        
        while(curr.next!=null){
            if(curr.data == val){
                if(curr==head){
                    head = curr.next;
                    curr = head;
                }else{
                    prev.next = curr.next;
                    curr = prev.next;
                }
            }else{
                prev = curr;
                curr = curr.next;
            }
        }
        return head;
    }
}

在主要运行以下代码。

        LinkedListPer dh = new LinkedListPer();
        ListNode<Integer> ln = null;
        ln = dh.add(ln, 1);
        ln = dh.add(ln,2);
        ln = dh.add(ln,1);
        ln = dh.add(ln,4);
        ln = dh.add(ln,6);
        
        dh.display(ln);
        
        ln = dh.delete(ln, 1);
        dh.display(ln);

我得到了以下输出:

1->2->1->4->NULL

2->4->NULL

现在来看一下见解:早些时候在执行delete()操作时,我没有保存相同的返回值。即我只写了以下声明。

dh.delete(ln,1)

输出如下:

1->2->1->4->NULL

1->2->4->NULL

所以,我想这完全符合你面临的问题的行为。我认为问题在于执行删除操作后使用head

相关内容

  • 没有找到相关文章

最新更新