code: public static IAtomContainer scaffold(final IAtomContainer mol) {
  
          // Old AtomContainer IMPL, cannot work with this
          if (!mol.isEmpty() && mol.getAtom(0).getContainer() == null)
              return null;
  
          Deque<IAtom> queue = new ArrayDeque<>();
          int[] bcount = new int[mol.getAtomCount()];
  
          // Step 1. Mark and queue all terminal (degree 1) atoms
          for (IAtom atom : mol.atoms()) {
              int numBonds = atom.getBondCount();
              bcount[atom.getIndex()] = numBonds;
              if (numBonds == 1)
                  queue.add(atom);
          }
  
          // Step 2. Iteratively remove terminal atoms queuing new atoms
          //         as they become terminal
          while (!queue.isEmpty()) {
              IAtom atom = queue.poll();
              if (atom == null)
                  continue;
              bcount[atom.getIndex()] = 0;
              for (IBond bond : atom.bonds()) {
                  IAtom nbr = bond.getOther(atom);
                  bcount[nbr.getIndex()]--;
                  if (bcount[nbr.getIndex()] == 1)
                      queue.add(nbr);
              }
          }
  
          // Step 3. Copy out the atoms/bonds that are part of the Murcko
          //         scaffold
          IAtomContainer scaffold = mol.getBuilder().newAtomContainer();
          for (int i = 0; i < mol.getAtomCount(); i++) {
              IAtom atom = mol.getAtom(i);
              if (bcount[i] > 0)
                  scaffold.addAtom(atom);
          }
          for (int i = 0; i < mol.getBondCount(); i++) {
              IBond bond = mol.getBond(i);
              if (bcount[bond.getBegin().getIndex()] > 0 &&
                  bcount[bond.getEnd().getIndex()] > 0)
                  scaffold.addBond(bond);
          }
          return scaffold;
      }
docs: Computes the Murcko Scaffold for the provided molecule in linear time.
  Note the return value contains the same atoms/bonds as in the input
  and an additional clone and valence adjustments may be required.
  
  @param mol the molecule
  @return the atoms and bonds in the scaffold