0

I'm trying to remove the last node from a linkedList and return it. This is part of a Linkedlist class. The following method that I wrote doesn't delete the last node. Does anybody know why?

public int delete(){

    if(front==null){

        throw new NoSuchElementException(); 

    }else{

       ListNode current = front;

       while(current.next!=null){
          current = current.next;   
       }

       int delete = current.data;
       current = null;
       return delete;
    } 
}
Kasie Sami
  • 55
  • 8

6 Answers6

3

Setting current to null only changes the reference to null. It in no way affects the linked list data structure. You need to find the second to last node and set its next pointer to null:

int data = secondToLastNode.next.data;
secondToLastNode.next = null;
return data;

Of course, you'll need to handle the situation where there is only one node in the list which the above code doesn't account for.

Chris Knight
  • 24,333
  • 24
  • 88
  • 134
2

You are only setting your local reference current to null; you're not changing your list.

Assuming this is a singly linked list, you will need to set the second-to-last ListNode's next to null (or set front to null if it's the only item).

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

There are three situations you need to cover:

  1. There are no entries in your list. Usually you just exit in this case but throwing an exception like you do should be fine.
  2. There is only one entry in your list. In this case your variable front will have a value but front.next will be null. You should set front to null in this case.
  3. For none of the above you should set the next of the last but one entry to null. You have not managed to do this yet.
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213
0

I wound up explaining this here.

Jave is a pass-by-reference language and = reassigns the reference. You're only changing local references, see sample code in above link and understand that.

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • That's only half true, http://stackoverflow.com/questions/40480/is-java-pass-by-reference. – Chris Mar 05 '13 at 23:30
  • @Chris yes, this is explained in full in linked answer, the terser explanation I came up with was to call it pass by ref and explain `=`'s specialness. – djechlin Mar 05 '13 at 23:31
0

Remember the 'current' before the one that has the null pointer, and set the 'next' pointer of that node to null. That way, you delete the reference to the latest node, instead of just updating a local variable.

Rick
  • 443
  • 2
  • 10
0

Try this one

public int delete(){

    if(front==null){

        throw new NoSuchElementException(); 

    } else if(front.next===null){
           return front.data;
}else{

       ListNode current = front;

       while(current.next.next!=null){
          current = current.next;   
       }

       int deleted_node = current.next.data;
       current.next = null;
       return deleted_node;
    } 
}
Krishna Kamal
  • 751
  • 2
  • 10
  • 21