简体   繁体   English

从单链列表中删除最后一个节点(java)

[英]removing the last node from singly linked list (java)

My method int deletLast() should delete the last node as well as return the value inside the node being deleted. 我的方法int deletLast()应该删除最后一个节点,并返回要删除的节点内部的值。 my code does not seem to work. 我的代码似乎不起作用。 it does not delete the last node. 它不会删除最后一个节点。 any help will be greatly appreciated. 任何帮助将不胜感激。

import java.util.NoSuchElementException; 导入java.util.NoSuchElementException; import java.util.Scanner; 导入java.util.Scanner;

public class LinkedList11 { // Private inner class Node 公共类LinkedList11 {//私有内部类Node

private class Node{
    int data;
    Node link;


    public Node(){
        data = Integer.MIN_VALUE;
        link = null;
    }

    public Node(int x, Node p){
        data = x;
        link = p;
    }
}
// End of Node class

public Node head;

public LinkedList11(){
    head = null;
}

public int deleteLast() throws NoSuchElementException {


    if ( head == null )   //handle when list is empty
    { throw new NoSuchElementException();}

     if(head.link == null)  //handle when head is the only node
     { return head.data;


        }

        Node position = head;
        Node temp = head;   //temp has to be initialized to something 
        int dataAtEnd =0;
        while (position != null)
        {   dataAtEnd = position.data;    
            temp =position;               //safe keep current position
           position = position.link;     //update position pointer to get the next value  
          }

       position =temp;  // store current position in next position
       return dataAtEnd;

}

} }

first, if head is the only node and you want to remove it, you'll need to set head null. 首先,如果head是唯一的节点,并且要删除它,则需要将head设置为null。

if(head.link == null) {
    int result = head .data;
    head = null;
    return result;
}

Try something like this after checking if head is the only node: 在检查head是否是唯一节点之后,尝试以下操作:

Node current = head;
while (current.link.link != null)
    current = current.link;
int result = current.link.data;
current.link = null;
return result;

Bc you need to look to steps ahead to check if next node is last one and remove last from the one before the last. BC,您需要检查步骤以检查下一个节点是否为最后一个节点,并从最后一个节点之前的一个节点中删除最后一个节点。 I hope you understand,what I meant and sorry for typos 希望您理解我的意思,对错字表示抱歉

remove line "return head.data;" 删除行“ return head.data;” which is right above where you get the error. 在您得到错误的位置上方。 "ead = null; //gives error " gives unreachable because you have a return statement right above, so it is obviously unreachable “ ead = null; //给出错误”给出了不可访问的信息,因为您在上面有一个return语句,因此显然无法访问

`public int deleteLast() throws NoSuchElementException { `public int deleteLast()抛出NoSuchElementException {

if ( head == null )   //handle when list is empty
{ throw new NoSuchElementException();}

 if(head.link == null)  //handle when head is the only node
 { 
     // You must store the data somewhere since head has to be set to NULL.
     int dataToReturn = head.data;

     // Since head is the only node, set it to NULL now.
     head = null;

     // Now return the data the the last node (head in this case) contained.
     return dataToReturn;
    }

    Node position = head;
    Node temp = head;   //temp has to be initialized to something 
    int dataAtEnd =0;

    while (position.link != null)
    {   dataAtEnd = position.data;    
        temp =position;               //safe keep current position
       position = position.link;     //update position pointer to get the next value  
      }

   position = null;
   temp.link = null;//this is what deletes the last node.

   return dataAtEnd;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM