@classmethod
    def __build_and_validate_linked_list(cls, nodes):
        # check if we're working with no nodes, return None if so
        # don't error/exit because and empty list is not necessarily invalid
        if len(nodes) == 0:
            return None

        heads = []
        backrefs = {}
        for node in nodes:
            if node.backref is not None:
                # Check for duplicate refs
                backnodes = [n for n in nodes if n.id == node.backref]
                if len(backnodes) == 1:
                    node.backref = backnodes[0]
                elif len(backnodes) > 1:
                    msg = "\n".join(["Duplicate refs found in %s\n" % b.filename for b in backnodes])
                    raise DuplicateRefsError(msg)
                else:
                    raise MissingRefError("Backref points to non-existent alter: %s\n" % node.filename)

                # catalog backrefs (for divergence checking)
                if node.backref.id not in backrefs:
                    backrefs[node.backref.id] = []
                backrefs[node.backref.id].append(node)
            else:
                heads.append(node)

        # check backref catalog for duplicates
        for (_, _nodes) in backrefs.iteritems():
            if len(_nodes) > 1:
                msg = []
                msg.append("Divergent Branch:"
                           "\nThis means that we have found alters that share a common parent. To fix"
                           "\nthis you can run the 'resolve' command. When merging changes from your"
                           "\nfeature-branch, ensure that you resolve your files that are in conflict"
                           "\n(not existing files that were previously in a good state)."
                           "\n")
                msg += ["\tDuplicate backref found (divergent branch): %s\n" % node.filename for node in _nodes]
                msg = "\n".join(msg)
                raise DuplicateRefsError(msg)

        # check head(s)
        if len(heads) > 1:
            msg = ["More than one head found:"] + ["  %s" % head.filename for head in heads]
            msg = "\n".join(msg)
            raise HeadError(msg)
        elif len(heads) == 0:
            raise HeadError("No head found")

        # check tail(s)
        tails = []
        for node in nodes:
            if node.backref is None:
                continue
            children = [n for n in nodes if n.backref == node]
            if len(children) == 0:
                tails.append(node)

        if len(tails) > 1:
            msg = "\n".join(["Duplicate backref found in %s\n" % tail.filename for tail in tails])
            raise DuplicateRefsError(msg)

        elif len(tails) == 0 and (not len(nodes) == 1):
            raise CircularRefError("something strange is happening... no last alter found (circular references!!)")

        if len(nodes) == 1:
            return heads[0]
        else:
            return tails[0]
Build a linked list and validate it's correctness given an array of
dataset/ETHPy150Open appnexus/schema-tool/schematool/util/chain.py/ChainUtil.__build_and_validate_linked_list