const findFirstNode = (nodeList) => {
// TODO write your impl here
// 拿到所有指向的指针
if (!(nodeList && nodeList.length > 0)) {
return '链表为空';
}
const nextIds = nodeList.map(n => n.nextId);
const heads = nodeList.filter(n => !nextIds.include(n.id));
if (heads.length === 0) {
return '链表为环';
}
if (heads.length === 1) {
return `头结点为:${JSON.stringify(heads[0])}`;
}
if (heads.length > 1) {
return `头结点有多个,分别为:${heads.map(h => JSON.stringify(h)).join(',')}`;
}
};