Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LinkedList中查找交集-Javascript_Javascript_Algorithm_Data Structures - Fatal编程技术网

在LinkedList中查找交集-Javascript

在LinkedList中查找交集-Javascript,javascript,algorithm,data-structures,Javascript,Algorithm,Data Structures,下面是我试图在两个链表中找到交点的地方: 它适用于相同大小的列表,但是我不明白为什么它不适用于不同长度的列表。我推进较长列表的指针(以说明长度的差异),但在检查值时出现了问题。这是linkedlist交叉点的一个示例: 我的做法如下: 浏览每个列表并计算长度 比较尾部,如果尾部不同,则没有交点 将两个指针设置为列表的开头-->我怀疑这是我的主要问题 将较长linkedlist上的指针向前移动长度差(辅助函数“advanceToKthNode”在此处是相关的),以忽略不相关的节点 遍历每个列表

下面是我试图在两个链表中找到交点的地方:

它适用于相同大小的列表,但是我不明白为什么它不适用于不同长度的列表。我推进较长列表的指针(以说明长度的差异),但在检查值时出现了问题。这是linkedlist交叉点的一个示例:

我的做法如下:

  • 浏览每个列表并计算长度
  • 比较尾部,如果尾部不同,则没有交点
  • 将两个指针设置为列表的开头-->我怀疑这是我的主要问题
  • 将较长linkedlist上的指针向前移动长度差(辅助函数“advanceToKthNode”在此处是相关的),以忽略不相关的节点
  • 遍历每个列表,直到每个指针上的值相同
  • 这是我的主要职能:

        LinkedList.prototype.isIntersection = function (LL1, LL2) {
      var tail1 = LL1.tail;
      var tail2 = LL2.tail;
      var length1 = LL1.getLength(LL1.head);
      var length2 = LL2.getLength(LL2.head);
    
      if (tail1.val !== tail2.val) {
    
        return false;
      }
    
      var shorter = ((length1 > length2) ? LL2 : LL1);
    
      var longer = ((length1 > length2) ? LL1 : LL2);
    
      var difference = Math.abs(length1 - length2);
    
      if (difference > 0) {
    
        //advance the longer one if there's a difference. this works.
        longer = this.advanceToKthNode(longer, difference);
        console.log(longer)
    
      }
    
      //point to the heads so you can iterate.
      var shortIterator = shorter.head;
      // this dosn't have a head if you advanced it.
      var longIterator = longer.head || longer.val;
    
      console.log(longIterator);
      // console.log(shortIterator);
      var intersection = "No Intersection";
      //don't count the tail as an intersection.
      while (longIterator && shortIterator && (longIterator !== longer.tail)) {
        console.log("LONG", longIterator)
        console.log("SHORT", shortIterator)
        if (longIterator.val === shortIterator.val) {
          intersection = longIterator.val;
        }
        shortIterator = shortIterator.next;
        longIterator = longIterator.next;
      }
    
      // while ((longIterator && shortIterator)&&longIterator.val !== shortIterator.val) {
      //   // console.log(longIterator);
      //   shortIterator = shortIterator.next;
      //   longIterator = longIterator.next;
      // }
    
      return intersection;
    };
    

    所以你要比较的链表有一棵树的结构,在某一点上它们是相同的?没有这样想!我试图通过学习如何做来练习LinkedList问题,你所说的交集到底是什么意思?这就是我想问的,图片显示了一个场景,你必须找到他们加入的节点,其余的都是一样的。这意味着您可以反转两个列表,只需比较节点,直到它们不再相同。基本上是指两个链接列表在一个特定节点上“重叠”的点。尾部总是相同的,但不一定是交集和尾部之间的所有内容。因此,您试图比较的链表具有树的结构,并且在某个点上它们将是相同的?没有这样想!我试图通过学习如何做来练习LinkedList问题,你所说的交集到底是什么意思?这就是我想问的,图片显示了一个场景,你必须找到他们加入的节点,其余的都是一样的。这意味着您可以反转两个列表,只需比较节点,直到它们不再相同。基本上是指两个链接列表在一个特定节点上“重叠”的点。尾部将始终相同,但不一定是交点和尾部之间的所有内容。