Exclude individual nav link if the page has a child

I’ve been racking my brain on this one and I’m sure the solution is simple.

What I’d like to do is exclude a link to a parent page if it contains a child. Right now I’m getting an error where the nav produces a duplicate item, but I’m not sure how to exclude a linked page if it contains a child. Here’s the code that I’m currently using:

        <?php $items = $pages->visible(); if($items->count()): ?>
      <div class="collapse navbar-collapse" id="navcol-1">
          <ul class="nav navbar-nav navbar-right">
            <?php foreach($items as $item): ?>
                <li class="menu-item<?= r($item->isOpen(), ' active') ?>" role="presentation">
                  <a <?php e($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo $item->title()->html() ?></a>
                </li>
                <?php $children = $item->children()->visible(); if($children->count() > 0): ?>
                <li class="dropdown">
                  <a href="#" class="dropdown-toggle <?php e($item->isOpen(), 'active') ?>" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $item->title()->html() ?> <span class="caret"></span></a>
                  <ul class="dropdown-menu">
                    <?php foreach($children as $child): ?>
                      <li><a<?php e($child->isOpen(), ' class="active"') ?> href="<?php echo $child->url() ?>"><?php echo $child->title()->html() ?></a></li>
                    <?php endforeach ?>
                  </ul>
                </li>
                <?php endif ?>
            <?php endforeach ?>
          </ul>
      </div>
    <?php endif ?>

And here’s a screenshot for illustration purposes:

I think it should be like this:

<?php 
$items = $pages->visible(); 
if($items->count()): ?>
  <div class="collapse navbar-collapse" id="navcol-1">
    <ul class="nav navbar-nav navbar-right">
      <?php foreach($items as $item): ?>
        <?php if(! $item->hasChildren()): ?>
          <li class="menu-item<?= r($item->isOpen(), ' active') ?>" role="presentation">
            <a <?php e($item->isOpen(), ' class="active"') ?> href="<?php echo $item->url() ?>"><?php echo $item->title()->html() ?></a>  
          </li>
        <?php else: ?>
          <?php $children = $item->children()->visible(); ?>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle <?php e($item->isOpen(), 'active') ?>" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"><?php echo $item->title()->html() ?> <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <?php foreach($children as $child): ?>
                  <li><a<?php e($child->isOpen(), ' class="active"') ?> href="<?php echo $child->url() ?>"><?php echo $child->title()->html() ?></a></li>
                <?php endforeach ?>
              </ul>
            </li>
        <?php endif ?>
      <?php endforeach ?>
    </ul>
  </div>
<?php endif ?>

Within the first foreach-loop, check if the item has children, if not, create the link for the item. If the page has children, create the dropdown menu.

Thanks for the reply! When I paste this in place I end up getting a syntax error, unexpected ‘endforeach’ (T_ENDFOREACH). Any thoughts? Apologies, my php skills are primitive!

Sorry, I left an extra line, corrected it in my code above.

Works great! Thank you SO much!