code: public final void replaceChild(Node child, Node newChild) {
      checkArgument(newChild.next == null, "The new child node has next siblings.");
      checkArgument(newChild.previous == null, "The new child node has previous siblings.");
      checkArgument(newChild.parent == null, "The new child node already has a parent.");
      checkState(child.parent == this, "%s is not the parent of %s", this, child);
  
      // Copy over important information.
      newChild.useSourceInfoIfMissingFrom(child);
      newChild.parent = this;
  
      Node nextSibling = child.next;
      Node prevSibling = child.previous;
  
      Node last = first.previous;
  
      if (child == prevSibling) {  // first and only child
        first = newChild;
        first.previous = newChild;
      } else {
        if (child == first) {
          first = newChild;
          // prevSibling == last, and last.next remains null
        } else {
          prevSibling.next = newChild;
        }
  
        if (child == last) {
          first.previous = newChild;
        } else {
          nextSibling.previous = newChild;
        }
  
        newChild.previous = prevSibling;
      }
      newChild.next = nextSibling;  // maybe null
  
      child.next = null;
      child.previous = null;
      child.parent = null;
    }
docs: Detaches child from Node and replaces it with newChild.