SOURCE

const findFirstNode = (nodeList) => {
    const idNodeObject = nodeList.reduce((p, node) => {
      p.idArray.push(node.id);
      p.nextIdArray.push(node.nextId);
      return p;
    }, { idArray: [], nextIdArray: [] });
    let error = false; let head = false; let end = false; let headNode;
    for (let i = 0; i < nodeList.length; i++) {
      // 如果nextId不在id数组里,并且id也不在nextId数组里,异常
      // 如果nextId不在id数组里,且id在nextId里,是链尾,不是circle
      // 如果nextId在id数组里,且id不在nextId里,是链头
      const nextIdIndex = idNodeObject.idArray.indexOf(nodeList[i].nextId);
      const idIndex = idNodeObject.nextIdArray.indexOf(nodeList[i].id);
      if (nextIdIndex === -1 && idIndex === -1) {
        error = true;
      } else if (nextIdIndex === -1 && idIndex > -1) {
        end = true;
      } else if (nextIdIndex > -1 && idIndex === -1) {
        headNode = nodeList[i];
        head = true;
      }
    }
    if (!error && head && end) {
      return headNode.id;
    } if (!error && !head && !end) {
      console.log("是环状链表");
    } else {
      console.log("非正常链表");
    }
  }
const list = [{"id":3,"nextId":0},{"id":1,"nextId":2},{"id":2,"nextId":3}];
//输出 1
console.log(findFirstNode(list));
console 命令行工具 X clear

                    
>
console