如何在java中删除链表的最后一个节点?
我需要在Java中实现LinkedList的removeFirst和removeLast两个方法 第一种方法是这样解决的:如何在java中删除链表的最后一个节点?,java,data-structures,linked-list,Java,Data Structures,Linked List,我需要在Java中实现LinkedList的removeFirst和removeLast两个方法 第一种方法是这样解决的: @Override public E removeFirst() { if(isEmpty()){ throw new NoSuchElementException(); } E element = top.next.data; top.next = top.next.next; numElements--;
@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++) {
}
}
public E removeLast(){
if(isEmpty()){
抛出新的NoTouchElementException();
}
对于(int i=0;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();
}
公共类节点{
E数据;
节点下一步;
公共节点(E数据){
这是(数据,空);
}
公共节点(E数据,节点下一个){
这个数据=数据;
this.next=null;
}
@凌驾
公共字符串toString(){
返回data.toString();
}
}此逻辑应起作用-
Node <E> tmp;
tmp = next;
while(tmp.next.next != null)
tmp = tmp.next;
tmp.setNext(null);
节点tmp;
tmp=下一个;
while(tmp.next.next!=null)
tmp=tmp.next;
tmp.setNext(空);
如果我们有1->3->4->null
当它达到3时,它应设置为空
因此,新数组看起来像这样1->3->null
removeFirst
和removeLast
已经存在于LinkedList
类()中。那么就不需要从头开始创建它们了。类似的东西可能会起作用,如果没有看到您的list类,很难说,因为我无法测试它:
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;
}
}
}
public E removeLast(){
if(isEmpty()){
抛出新的NoTouchElementException();
}
节点=顶部;
while(true){
Node nextNode=Node.next;
if(nextNode.next==null){
node.next=null;
返回nextNode.data;
}否则{
node=nextNode;
}
}
}
但值得一提的是,通常在单链接列表中,您希望包含类保留尾部指针(否则无法有效地追加到末尾)。如果你这样做,你也需要更新你的尾巴
还有一条评论,如果你发现自己定期删除最后一个,你想切换到一个双链接列表……为什么不使用内置的java.util.LinkedList类呢?这是学校的项目还是别的什么?我们必须保持两个指针是先前的和最新的。因为我们保留了列表中元素数量的记录,所以我们可以使用for循环遍历列表,找到currentNode指针指向的最后一个节点和previousNode指针指向的前一个节点。最后,将上一个下一个指针更新为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;
public E removeLast(){
if(isEmpty()){
抛出新的NoTouchElementException();
}
节点previousNode=顶部;
节点当前节点=顶部;
对于(int i=0;i
}Read.Hi,您应该找到最后两个元素,并在倒数第二个元素上设置
next=null
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;
}