def _CollapseAddressListRecursive(addresses):
  """Recursively loops through the addresses, collapsing concurrent netblocks.

  Example:

    ip1 = IPv4('1.1.0.0/24')
    ip2 = IPv4('1.1.1.0/24')
    ip3 = IPv4('1.1.2.0/24')
    ip4 = IPv4('1.1.3.0/24')
    ip5 = IPv4('1.1.4.0/24')
    ip6 = IPv4('1.1.0.1/22')

    _CollapseAddressListRecursive([ip1, ip2, ip3, ip4, ip5, ip6]) ->
    [IPv4('1.1.0.0/22'), IPv4('1.1.4.0/24')]

  This shouldn't be called directly; it is called via CollapseAddrList([]).

  Args:
    addresses: A list of IPv4 or IPv6 objects.

  Returns:
    A list of IPv4 or IPv6 objects depending on what we were passed.
  """
  ret_array = []
  optimized = False

  for cur_addr in addresses:
    if not ret_array:
      ret_array.append(cur_addr)
      continue
    if ret_array[-1].Contains(cur_addr):
      optimized = True
    elif cur_addr == ret_array[-1].Supernet().Subnet()[1]:
      ret_array.append(ret_array.pop().Supernet())
      optimized = True
    else:
      ret_array.append(cur_addr)

  if optimized:
    return _CollapseAddressListRecursive(ret_array)

  return ret_array