1

I want to remove the required values(int) in a linked list. For example, {3,1,2,3,3}. I use remove(int 3), then it should be {1,2}. Can you help me, my code can just remove the 3 in the index 0, but I still need to remove the index 3 and 4.

public void remove(int value) {
    IntegerNode curr = head;
    IntegerNode prev = null;

    for(curr = head; curr != null; curr = curr.next) {
        if(curr.item == value) {
            break;
        }

        prev = curr;
    }

    if(prev == null) {
        head = curr.next;
    } else {
        prev.next = curr.next;
    }

    count--;
}
Levent Divilioglu
  • 11,198
  • 5
  • 59
  • 106
Jimmy Tan
  • 89
  • 1
  • 8

4 Answers4

1

your code is good but you forgot to check all elements because in for loop when the first element 3 is found will get into break , so it won't check the rest of 3s elements . try this code ,also you don't need break just when you find this element delete it and go to the next:

PS : The count variable you need to minimize it after every deletion process , in your code it will be executed just for one time .

public void remove(int value) {
    IntegerNode curr = head;
    IntegerNode prev = null;

        for (curr = head; curr != null; curr = curr.next) {
            if(curr.item == value) {
                if (prev == null) {
                    head = curr.next;
                } else {
                    prev.next = curr.next;
                }
                    count--;
            }
            prev = curr;
        }
}
Zizoo
  • 1,694
  • 5
  • 20
  • 42
  • 1
    if my answer solve your problem please select it as an answer don't leave the question unanswered .good luck – Zizoo Oct 02 '15 at 12:20
1

Just one line:

while(list.remove(new Integer(3))){}

Just test it (to be sure) with next code:

LinkedList<Integer> list =new LinkedList<Integer>();
list.add(1);
list.add(2);
list.add(3);
list.add(3);
list.add(3);
list.add(2);
System.out.println(list);
while(list.remove(new Integer(3))){}
System.out.println(list);

Result:

[1, 2, 3, 3, 3, 2]
[1, 2, 2]
miken32
  • 42,008
  • 16
  • 111
  • 154
0

The method boolean remove(Object) returns true in the case the element is in the list. You could do something like (in an uncompressed form on purpose):

boolean condition = true;
while(condition){
     condition = list.remove(3);
}

The condition will ensure that each unwanted element is deleted in the list. Pay attention to the method, I do not know exactly the precedence, but there is also E remove(int i) that may lead you to delete the element at the i-th position and not the wanted one. Assigning the value to a boolean, as above, could work in disambiguating things, but in the other case use:

list.remove(new Integer(3));

that will work as highlighted in this question: Properly removing an Integer from a List<Integer>

Community
  • 1
  • 1
Lemm Ras
  • 992
  • 7
  • 18
-1

you can iterate through the list and check the availability of item using below method.

boolean contains(Object o)

Then if you found it, then you remove it since you already know the index.

Object remove(int index)

Janath
  • 137
  • 9