Java 从单链表中删除两个给定位置之间的节点?

Java 从单链表中删除两个给定位置之间的节点?,java,singly-linked-list,Java,Singly Linked List,我正在自学数据结构,并学习有关此主题的Java书籍。目前我正在学习链表的实现。我一直在努力研究如何编写一个方法,该方法采用“startPos”和“endPos”并简单地删除相应的节点。我正在验证“startPos”和“endPos”,以便捕获无效的位置输入。我在谷歌上搜索过方向,但没有遇到任何在线示例可以帮助我理解这个逻辑。我将非常感谢您的指导。多谢各位 class Node{ public Object data; public Node next; } 删除节点法 pu

我正在自学数据结构,并学习有关此主题的Java书籍。目前我正在学习链表的实现。我一直在努力研究如何编写一个方法,该方法采用“startPos”和“endPos”并简单地删除相应的节点。我正在验证“startPos”和“endPos”,以便捕获无效的位置输入。我在谷歌上搜索过方向,但没有遇到任何在线示例可以帮助我理解这个逻辑。我将非常感谢您的指导。多谢各位

class Node{

   public Object data;
   public Node next;

}
删除节点法

  public void deleteNodes( int startPos, int endPos ){         
      Node node = _nHead;
      int counter = 0;

  if( startPos < 1 || startPos > getSize() )
      return;

  if( endPos < 1 || endPos > getSize() ) 
      return;


  while( node != null){

    node = node.next;
    ++counter;
  }
}   

删除单链表上两个节点之间的所有节点并不是很难

您需要两个占位符。在链表中移动,直到找到开始节点,并将其中一个占位符设置为该节点。然后在链表的其余部分移动第二个占位符,直到找到第二个节点。将第一个节点的->下一个参数设置为与第二个节点相等,就可以有效地删除这两个节点之间的所有内容

为了进行正确的清理,您应该跟踪第一个节点之后的下一个节点,并释放从内存中删除的所有节点,但这在C语言中比Java语言中更为关键

对于双链接列表,该方法类似,只是您还必须将第二个节点的“上一个”设置为第一个节点

例如:

public void deleteNodes( int startPos, int endPos ){         
    Node node = _nHead;
    Node start;
    Node end;

    int counter = 0;

    if( startPos < 1 || startPos > getSize() )
        return;

    if( endPos < 1 || endPos > getSize() ) 
        return;

    if (endPos < startPos)
    {
        int placeholder = startPos;
        startPos = endPos;
        endPos = placeholder;   // switches end and start if start is greater than end
    }

    if (endPos == startPos)
        return; // if they are equal we aren't deleting anything;


    while( node != null)
    {
        if (counter == startPos)
            start = node;

        if (counter == endPos)
            end = node;

        node = node.next;
        counter++;
    }

    if (start != NULL && end != NULL)
    {
        start.next = end;
    }
}  
public void deleteNodes(intstartpos,intendpos){
节点节点=_nHead;
节点启动;
节点端;
int计数器=0;
if(startPos<1 | | startPos>getSize())
返回;
如果(endPos<1 | | endPos>getSize())
返回;
如果(结束位置<开始位置)
{
int占位符=startPos;
startPos=endPos;
endPos=placeholder;//如果start大于end,则切换end和start
}
if(endPos==startPos)
return;//如果它们相等,则不删除任何内容;
while(节点!=null)
{
如果(计数器==startPos)
开始=节点;
如果(计数器==endPos)
结束=节点;
node=node.next;
计数器++;
}
如果(开始!=NULL&&end!=NULL)
{
start.next=结束;
}
}  

您只需将移除范围开始处的节点的下一个指针设置为移除范围结束处的节点。因为在delete范围内没有对节点的引用,所以Java的垃圾收集应该清除它们

public void deleteNodes( int startPos, int endPos ){         
    Node node = _nHead;
    Node start;
    Node end;

    int counter = 0;

    if( startPos < 1 || startPos > getSize() )
        return;

    if( endPos < 1 || endPos > getSize() ) 
        return;

    if (endPos < startPos)
    {
        int placeholder = startPos;
        startPos = endPos;
        endPos = placeholder;   // switches end and start if start is greater than end
    }

    if (endPos == startPos)
        return; // if they are equal we aren't deleting anything;


    while( node != null)
    {
        if (counter == startPos)
            start = node;

        if (counter == endPos)
            end = node;

        node = node.next;
        counter++;
    }

    if (start != NULL && end != NULL)
    {
        start.next = end;
    }
}