LinkedList插入节点后

时间:2016-05-10 11:34:23

标签: java algorithm linked-list

我一直在尝试使用Java在当前节点之后将节点插入到链表中。我已经能够在当前节点之前获得插入但我无法使其工作:

以下是insertNodeBefore的代码。我只是想知道是否有一种方法可以调整它以便能够在当前节点之后插入?

// Insert new node nVal to the list before current node curVal 
public void insertNodeBefore(E nVal, E curVal) {
    Node<E> newNode = new Node<E>(nVal);

    Node<E> curr = head;
    Node<E> prev = null;

    if (head.getNodeValue() == curVal) {
        newNode.setNext(head);
        head = newNode;
        return;
    }

    // scan until locate node or come to end of list
    while (curr != null) {
        // have a match 
        if (curr.getNodeValue() == curVal) {
            // insert node
            newNode.setNext(curr);
            prev.setNext(newNode);
            break;
        } else {
            // advanced curr and prev
            prev = curr;
            curr = curr.getNext();
        }
    }
}

3 个答案:

答案 0 :(得分:4)

基本上找到节点然后:

  • 将新节点的下一个设置为当前
  • 将当前的下一个当前设置为新节点

这样的事情应该有效:

public void insertNodeBeAfter(E nVal, E curVal) {
    Node<E> newNode = new Node<E>(nVal);

    Node<E> curr = head;

    // scan until locate node or come to end of list
    while (curr != null) {
        // have a match 
        // Replaced == with .equals
        if (curr.getNodeValue().equals(curVal)) {
            // insert node
            newNode.setNext(curr.getNext());
            curr.setNext(newNode);
            break;
        } else {
            curr = curr.getNext();
        }
    }
}

注意不需要对头部进行不同的处理,因为它不会发生变化。

注意:如果列表为空或未找到curVal,例如抛出RuntimeException,可能会非常有趣地处理非静默。

答案 1 :(得分:2)

我尝试将遍历代码和插入代码分开,它全部取自您提问的代码。

private void insert(Node<E> prev, Node<E> after, Node<E> newNode) {
    newNode.setNext(after);
    if (prev != null) {
        prev.setNext(newNode);
    } else {
        head = newNode;
    }
}
public Node<E> findPrevOf(E curVal) {
    Node<E> curr = head;
    Node<E> prev = null;
    // scan until locate node or come to end of list
    while (curr != null) {
        // have a match 
        if (curr.getNodeValue() == curVal) {
            return prev;
        } else {
            // advanced curr and prev
            prev = curr;
            curr = curr.getNext();
        }
    }
    throw new Exception("throw an exception to indicate that value does not exist");
}
public void insertNodeAfter(E nVal, E curVal) {
    Node<E> prev = findPrevOf(curVal);
    Node<E> curr = null;
    if (prev == null) {
        curr = head;
    } else {
        curr = prev.getNext();
    }
    Node<E> newNode = new Node<E>(nVal);
    insert(curr, (curr == null ? curr: curr.getNext()), newNode);
}
// Insert new node nVal to the list before current node curVal 
public void insertNodeBefore(E nVal, E curVal) {
    Node<E> prev = findPrevOf(curVal);
    Node<E> newNode = new Node<E>(nVal);
    insert(prev, (prev == null ? head : prev.getNext()), newNode);
}

答案 2 :(得分:1)

在给定节点之后插入实际上比在给定节点之前插入更容易:

// Insert new node nVal to the list after current node curVal 
public void insertNodeAfter(E nVal, E curVal) {
    Node<E> newNode = new Node<E>(nVal);
    Node<E> curr = head;

    // scan until curValnode is found or the end of the list is reached
    while (curr != null) {
        // have a match 
        if (curr.getNodeValue().equals(curVal)) {
            // insert node
            newNode.setNext(curr.getNext());
            curr.setNext(newNode);
            break;
        } else {
            // advanced curr
            curr = curr.getNext();
        }
    }
}