I am trying make a linkedlist remove() method. It only works half way. When I try to remove a number, it removes the whole right side. For example here is the output: before remove: 8,7,3,5,2, after remove: 8,7,

How do I get it so only the three is removed?

Here is the code:

ListNode:

public class ListNode{
    private Object data;
    private ListNode next;

    public ListNode(){
        data = null;
        next = null;
    }
    public ListNode(Object d, ListNode n){
         data = d;
         next = n;
    }
    public Object getData(){
        return data;
    }
    public void setData(Object da){
        data = da;
    }
    public ListNode getNext(){
        return next;
    }
    public void setNext(ListNode l){
        next = l;
    }
    public String toString(){
        return data.toString();
    }
}

MyLinkedList:

   public class MyLinkedList{
      private ListNode head;
      private int size;

      public MyLinkedList(){
         head = null;
         size = 0;
      }

      public MyLinkedList(ListNode h){
         head = h;
         size = 0;
      }

      public void setLength(int s){
         size = s;
      }

      public void setListNode(ListNode n){
         head = n;
      }

      public ListNode getListNode(){
         return head;
      }

      public boolean add(Object obj){
         try{
            ListNode newNode = new ListNode(obj,head);
            head = newNode;
            size++;
            return true;
         } 
            catch(Exception evt){
               return false;
            }
      }

      public boolean remove(Object obj){
         ListNode temp = head;
         while(temp != null){
                if(temp.getNext().getData().equals(obj)){
                    temp.setNext(null);
                    size--;

                }
            temp = temp.getNext();
            }
            return true;
      }

        public String toString(){
            ListNode temp = head;
            String s = "";
            while(temp != null){
                s += temp.toString() + ",";
                temp = temp.getNext();
            }
            return s;
        }
   }

And the MyLinkedListDriver class:

public class MyLinkedListDriver{
    public static void main(String[] args){ 
        Integer three = new Integer(3);
        MyLinkedList list = new MyLinkedList();
        list.add(new Integer(2));
        list.add(new Integer(5));
        list.add(new Integer(3));
        list.add(new Integer(7));
        list.add(new Integer(8));

        System.out.println("before remove: " + list);
        list.remove(3);
        System.out.println("after remove: " + list);
    }
}

When I try to remove a number, it removes the whole right side.

That is what you should expect when you do temp.setNext(null) because nulling the link of temp makes temp the end of the list. If you want to remove just one element then you need to get the link of the element you want to remove and copy it into the link of the previous element.

@bguild
Thanks for the quick responce. Also how would I copy the link of the previous element?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.