【问题标题】:Finding a cycle in singly linked list with javascript (Is my solution efficient)使用javascript在单链表中查找循环(​​我的解决方案有效吗)
【发布时间】:2016-07-19 01:20:54
【问题描述】:

如果起始节点正确传递给函数,我的解决方案会很好。我想知道我的解决方案是否良好且有效。如果循环通过第一个节点作为参数传递给的函数存在,我应该能够返回 true。我想知道我的解决方案是否有效,尤其是对于面试环境。我在代码中的 cmets 是不言自明的。我使用变量 track 遍历列表并检查 null 或 head 作为下一个。如果我遇到其中任何一个遍历结束,然后单独检查 null 或 head 条件,并基于此返回适当的布尔值。

function SLLNode(elem) {
    this.value=elem;
    this.next=null;
}

var hasCycle=function(node){

    var track=node;
    //traverse thru list till next node is either null or back to first node
    while(track.next!==null && track.next!==this.head){
        track=track.next;
    }
    if(track.next === null){ //if next node null then no cycle
        return false;
    }
    if(track.next===this.head){ //if next node head then there is cycle
        return true;
    }
}

var my_node1=new SLLNode(3);
var my_node2=new SLLNode(5);
var my_node3=new SLLNode(19);

//assigning head
var head=my_node1;

//connecting linked list
my_node1.next=my_node2;
my_node2.next=my_node3;
my_node3.next=my_node1; //cycle
console.log("Has cycle?: "+hasCycle(my_node1)); //outputs true as expected

var node1=new SLLNode(3);
var node2=new SLLNode(5);
var node3=new SLLNode(19);

//assigning head
var head1=node1;
node1.next=node2;
node2.next=node3;
console.log("Has cycle?: "+hasCycle(node1)); //outputs false as expected

【问题讨论】:

    标签: javascript linked-list


    【解决方案1】:

    您可以在https://en.wikipedia.org/wiki/Cycle_detection 上阅读更多关于循环检测的信息,但主要的收获是,如果您移动一个指针的速度是另一个指针的两倍,则可以识别出一个循环,因为快速指针最终会赶上另一个。这是js中可能的解决方案。

    function hasCycle(head) {
      var slow, fast;
    
      if(!head || !head.next) return false;
    
        slow = head;
        fast = head;
    
        if(head.next === head) return true;
    
        while(fast.next.next) {
    
          slow = slow.next;
          fast = fast.next.next;
    
          if(slow === fast) return true;
        }
    
      return false;
    

    }

    【讨论】:

      【解决方案2】:

      不是一个非常有效的解决方案,因为我使用的是地图,但如果您不想使用两个指针,这个解决方案很容易理解

      // Preparation code:
      class Node {
        constructor(value) {
          this.value = value;
          this.next = null;
        }
      }
      
      function hasCycle(head) {
        let node = head;
        let map={};
      
        while(node){
            if(map[node.value]){
            //return node or true
               return {"Found":node}
            }  
          else{
      
            map[node.value] = true;
          }
          node = node.next;
        }
      
        return "Not found";
      }
      
      const nodeA = new Node('A');
      const nodeB = nodeA.next = new Node('B');
      const nodeC = nodeB.next = new Node('C');
      const nodeD = nodeC.next = new Node('D');
      const nodeE = nodeD.next = new Node('E');
      console.log(hasCycle(nodeA)); // => null
      nodeE.next = nodeB;
      console.log(hasCycle(nodeA))

      【讨论】:

        【解决方案3】:

        JSON.stringify() 可用于检测循环链表。如果链表是循环的,CircularDetector 返回 true。

        function CircularDetector (head) {
          try {
            JSON.stringify(head);
            return false;
          } catch (e) {
            return true;
          }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-08-01
          • 1970-01-01
          • 2021-03-17
          • 1970-01-01
          • 1970-01-01
          • 2019-03-19
          • 2012-05-03
          • 1970-01-01
          相关资源
          最近更新 更多