【问题标题】:How can I remove the last node of a Linked List in java?如何在java中删除链接列表的最后一个节点?
【发布时间】:2020-03-13 16:20:55
【问题描述】:

我需要在Java中实现LinkedList的两个方法removeFirst和removeLast

第一种方法我是这样解决的:

@Override
public E removeFirst() {
    if(isEmpty()){
        throw new NoSuchElementException();
    }
    E element = top.next.data;
    top.next = top.next.next;
    numElements--;
    return element;
}

我在使用 removeLast 方法时遇到问题

     public E removeLast() {
     if(isEmpty()){
         throw new NoSuchElementException();
     }

      for (int i = 0; i < numElements;i++) {


      }

}

我的想法是使用 for 循环来查找最后一个元素,但我不知道之后该做什么

有什么建议吗?

我的节点类如下:

public class Node<E> {

E data;
Node<E> next; 

public Node(E data) {
    this(data,null);
}

public Node(E data, Node<E> next) {
    this.data = data;
    this.next = null;
}

@Override
public String toString () {
    return data.toString();


}

}

【问题讨论】:

  • 阅读this
  • 嗨,你应该找到最后两个元素并在倒数第二个元素上设置next=null

标签: java data-structures linked-list


【解决方案1】:

我们必须保持两个指针先前和当前。由于我们记录了列表中元素的数量,我们可以使用 for 循环遍历列表并找到 currentNode 指针指向的最后一个节点和 previousNode 指针指向的前一个节点。最后将之前的next指针更新为null,返回currentNode数据。

 public E removeLast() {
    if(isEmpty()){
        throw new NoSuchElementException();
    }
    Node previousNode = top;
    Node currentNode = top;
    for (int i = 0; i < numElements -1 ;i++) {
        previousNode = currentNode;
        currentNode = currentNode.next;
    }
    // removed the last element and return the data
    previousNode.next = null;
    numElements-- 
    return currentNode.data;

}

【讨论】:

    【解决方案2】:

    removeFirstremoveLast 已存在于 LinkedList 类 (LinkedList javadoc) 中。不需要从头开始创建它们。

    【讨论】:

      【解决方案3】:

      这样的事情可能会奏效,如果没有看到你的列表类就很难说,因为我无法测试它:

        public E removeLast() {
          if(isEmpty()){
              throw new NoSuchElementException();
          }
          Node<E> node = top;
          while (true) {
            Node<E> nextNode = node.next;
            if (nextNode.next == null) {
              node.next = null;
              return nextNode.data;
            } else {
              node = nextNode;
            }
          }
        }
      

      但值得添加的是,通常在单链表中,您希望包含类保留一个尾指针(否则您不能有效地附加到末尾)。如果你这样做,你也需要更新你的尾巴。

      还有一条评论,如果你发现自己经常删除最后一个,你想切换到一个双向链表......你为什么不只使用内置的 java.util.LinkedList 类?这是为了学校项目还是什么?

      【讨论】:

        【解决方案4】:

        这个逻辑应该起作用 -

        Node <E> tmp;
        tmp = next;
        
        
        while(tmp.next.next != null)
        tmp = tmp.next;
        tmp.setNext(null);
        

        所以如果我们有 1->3->4->null

        当它达到 3 时,它应该 setNext 为 null 所以新数组看起来像这样 1->3->null

        【讨论】:

          【解决方案5】:
          public node removelast() {
              if(isEmpty())
                  return null;
              node temp =head;
              node temp2 =head.getNext();
              while(temp!=null && temp2!=null) {
                  if(temp2.getNext()==null) {
                      tail=temp;
                      temp.setNext(null);
                  }
                  temp2=temp2.getNext();
                  temp=temp.getNext();
              }   
              if(head.getNext()==null)
                  tail=head;
              return tail;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2016-06-11
            • 1970-01-01
            • 2013-03-25
            • 2014-05-22
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多