简体   繁体   中英

Remove all nodes with a given value using Java

I need to remove all nodes with a given value. However, my code removes them, but the final result I get is if the value is at the head, it doesn't delete. How can I fix this problem?

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;
}

The code that you show is correct. I've added a little bit to get a complete example:

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

and

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));
    }
}

and it prints

3 5 3 2
5 2
2

So the problem must be somewhere outside of the code that you show.

I have tried to run the code on my system, but by making some changes. The program just runs fine :) But I found an insight which I think you will find interesting. Anyway the code goes like this below:

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;
    }
}

In ran the following codes in main.

        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);

I got the following output:

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

2->4->NULL

Now for the Insight: Earlier on performing the delete() operation, I didn't save the value returned by the same. ie I only wrote the following statement.

dh.delete(ln,1)

The output was as follows:

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

1->2->4->NULL

So, I suppose this exactly fits the behavior of the problem that you face. I think the problem lies in using the head after performing the delete operation(s).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM