How do you remove the first Node in a Linked List?

北城以北 提交于 2020-01-03 04:16:24

问题


Sup guys so I'm going over a few of my methods in my Linked List class and I'm getting a logical error when removing a node from a linked list. I was working on my removeFirst() method when I then encountered a error in my removeLast() method as well. The problem is that both remove the last item in the list. not sure why but here is my code.

Remove First Node

public T removeFirst() throws EmptyCollectionException
{
 // Checking to see if the List is empty or not
    if ( isEmpty() )
        throw new EmptyCollectionException("LinkedList");

    Node < T > temp  = contents;

    T  next = contents.getNext().getItem();

    contents = new Node ( next, contents );
    count--;

    return temp.getItem();
}



Remove Last Node

public T removeLast() // fixed
{
 // Checking to see if the List is empty or not
    if (isEmpty())
        throw new EmptyCollectionException("LinkedList");

    // Node<T> temp = contents;
    Node<T> current = contents;  
    Node<T> prev = null;        

    while (current.getNext() != null) 
    {
        prev = current; 
        current = current.getNext();
    } 

    prev.setNext(null); 

    count--;

    return current.getItem();

}

I've looked around the questions already posted but I can't seem to find the answer I'm looking for.
I Know that a node has at least two values
one to hold the data and another to hold the reference to the next node

That's what I think is going on for the first one. But when I call the methods one after another they both get rid off the last node. Idk I will look over my code and update this question if necessary. But can you guys see where I'm going wrong and point me in the right direction. Thank you.


回答1:


If you have a list A->B->C, A being the head ("contents") of your list, in order to remove it, you simply have to advance the pointer to B, i.e. the next node in your list:

public T removeFirst() throws EmptyCollectionException {
    // Checking to see if the List is empty or not
    if ( isEmpty() )
        throw new EmptyCollectionException("LinkedList");

    Node<T> first = contents;

    contents = contents.getNext();
    count--;

    return first.getItem();
}

Since you also need to return the data associated with the first node, you need to keep a temporary reference to it. (I called it first)




回答2:


public void removeFirst() {
        if (head == null)
              return;
        else {
              if (head == tail) {
                    head = null;
                    tail = null;
              } else {
                    head = head.next;
              }
        }
  }



回答3:


I think you need to add head node to your liked list class for defining frst node of list.

public void deleteFront()
{
 if (head!=null)
 head = head.Next;
}



回答4:


public T removeFirst() throws EmptyCollectionException {
 if (isEmpty())
    throw new EmptyCollectionException("LinkedList");

Node < T > temp  = contents;

T  next = contents.getNext().getItem();

contents = new Node ( next, contents );
count--;



  return temp.getItem();
}

In this method comment last three statement. then add below three lines

contents=contents.getNext()
count--;
return next;

For removing last node: Its looks fine to me.



来源:https://stackoverflow.com/questions/16507299/how-do-you-remove-the-first-node-in-a-linked-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!