【问题标题】:Most efficient way to remove a node on a single linked list?删除单个链表上的节点的最有效方法?
【发布时间】:2020-01-29 00:20:03
【问题描述】:

我正在实现一个整数链接列表,我想知道找到给定值并将其从列表中删除的最有效方法是什么。这是我目前所拥有的:

public class LinkedList {
    Node head;

  public void insert(int value){
    Node newNode = new Node();
    newNode.data = value;
    newNode.next = null;

    if(head==null){
      head = newNode;
    } else {
      Node iterator = head;
      while(iterator.next!=null){
        iterator = iterator.next;
      }
      iterator.next = newNode;
    }
  }

  public void display(LinkedList list){
    Node iterator = head;
    while (iterator!=null){
      System.out.println(iterator.data);
      iterator = iterator.next;
    }
  }

  public void remove(LinkedList list, int value){
    Node iterator = head;
    while(iterator!=null){
      if(iterator.next.data == value){
          iterator.next = iterator.next.next;
      } else{
        iterator = iterator.next;
      }
    }
  }
}


public class Node {
  int data;
  Node next;
}

【问题讨论】:

  • 会抛出 NPE。
  • 也许你应该从参数中删除 LinkedList 列表,因为你不使用它。
  • 正确,我得到了 NPE,我的代码有什么问题?
  • iterator.next 可以为空,iterator.next.next 也可以为空。另外,head 是什么?它没有在您的代码中定义。
  • 我刚刚添加了所有内容,谢谢

标签: java singly-linked-list


【解决方案1】:

我在这里添加 sn-p 以从 SinglyLinkedList 中删除节点。我更喜欢 forLoop 而不是 while;这就是我在 for 循环中添加 sn-p 的原因。

希望代码中的注释可以帮助您导航/试运行 sn-p。

public boolean remove(int value){ 
 Node oneBeforeValueNode;
 // Using for to iterate through the SinglyLinkedList
 // head → is the head of your SingleLinkedList
 for (Node node = head; node != null; node = node.next) {
      // Compare the value with current node
      if (value.equals(node.data)) {
            // if matches then unlink/remove that node from SinglyLinkedList
            // if its a first node in SingleLinkedList
            if(oneBeforeValueNode==null){
              // Considering there exists next element else SLL will be empty
              head = head.next;
            } else {
              // if there exists next element it SLL it will attach that element with previous one
              oneBeforeValueNode.next = node.next;
            }
            return true;
       }
     // Storing previous node from current
     // To use it once value is found to reference it to current.next(node.next)
     oneBeforeValueNode = node;
   }
 return false;
}

同时添加 while 变体;只是为了顺其自然。

public Node remove(Node head, int key) {
        Node current = head;
        Node previous = null;
        while (current != null) {
            if(current.data == key) {

                if(current == head) {
                    head = head.next;
                    current = head;

                } else {
                    previous.next = current.next;
                    current = current.next;
                }

            } else {
                previous = current;
                current = current.next;
            }
        }

        return head;
    }

【讨论】:

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