【问题标题】:Reverse a linkedlist using javascript使用javascript反转链表
【发布时间】:2019-07-27 17:45:17
【问题描述】:

这是一个单链表,我想反转它

我在 stackoverflow 中找到了this,但它对我没有帮助

它返回 1 而不是 [16,5,10,1]

我对反向链表的了解是

让第一个节点指向null

第二个节点指向第一个节点

第三个节点指向第二个节点

有人可以帮我弄清楚如何在我的代码中反转链表吗?

这是我的 JS:

class LinkedList {
  constructor(value) {
    this.head = {
      value: value,
      next: null
    };
    this.tail = this.head;
    this.length = 1;
  }
  append(value) {
    const newNode = {
      value: value,
      next: null
    }
    this.tail.next = newNode;
    this.tail = newNode;
    this.length++;
    return this;
  }
  prepend(value) {
    const newNode = {
      value: value,
      next: null
    }
    newNode.next = this.head;
    this.head = newNode;
    this.length++;
    return this;
  }
  printList() {
    const array = [];
    let currentNode = this.head;
    while (currentNode !== null) {
      array.push(currentNode.value)
      currentNode = currentNode.next
    }
    return array;
  }
  insert(index, value) {
    //Check for proper parameters;
    if (index >= this.length) {
      console.log('yes')
      return this.append(value);
    }

    const newNode = {
      value: value,
      next: null
    }
    const leader = this.traverseToIndex(index - 1);
    const holdingPointer = leader.next;
    leader.next = newNode;
    newNode.next = holdingPointer;
    this.length++;
    return this.printList();
  }
  traverseToIndex(index) {
    //Check parameters
    let counter = 0;
    let currentNode = this.head;
    while (counter !== index) {
      currentNode = currentNode.next;
      counter++;
    }
    return currentNode;
  }
  remove(index) {
    // Check Parameters      
    const leader = this.traverseToIndex(index - 1);
    const unwantedNode = leader.next;
    leader.next = unwantedNode.next;
    this.length--;
    return this.printList();
  }
  reverse() {
    let currentNode = this.head;
    var previous = null;

    while (currentNode) {
      // save next or you lose it!!!
      var save = currentNode.next;
      // reverse pointer
      currentNode.next = previous;
      // increment previous to current node
      previous = currentNode;
      // increment node to next node or null at end of list
      currentNode = save;
    }
    return this.printList()
  }
}

let myLinkedList = new LinkedList(10);
myLinkedList.append(5)
myLinkedList.append(16)
myLinkedList.prepend(1)
myLinkedList.insert(2, 99)
myLinkedList.remove(2)
myLinkedList.reverse() //should return [16,5,10,1]

【问题讨论】:

    标签: javascript linked-list


    【解决方案1】:
    function reversesll(sll){
    
      if(!sll.head || !sll.head.next) return sll;
    
      var nodes=[], 
        current = sll.head;
      //storing all the nodes in an array
      while(current){
        nodes.push(current);
        current = current.next;
      }
    
      var reversedLL = new LinkedList();
    
      reversedLL.head = nodes.pop();
      current = reversedLL.head;
    
      var node = nodes.pop();  
      //make sure to make next of the newly inserted node to be null
      //other wise the last node of your SLL will retain its old next.
      while(node){
         node.next = null;
         current.next = node;
    
         current = current.next;
         node = nodes.pop();
      }
      return reversedLL;
    }
    
    //create the LL
    var sll = new LinkedList();
    sll.push(1);
    sll.push(2);
    sll.push(3);
    sll.push(4);
    sll.push(5);
    
    //test it
    reversesll(sll);
    //{head: {value:5, next:{value: 4, next: {value: 3, next: {value:2, next:{value:1, next: null}}}}}}
    

    【讨论】:

    • 看来你没听懂我的意思。我的课上已经有一个反向函数了。只是不要创建一个新的
    • 让我再检查一遍
    【解决方案2】:

    你实际上已经很接近了,在你的循环重新设置头部和尾部之后:

     while(/*...*/ { /*...*/ }
    
     this.tail = this.head;
     this.head = previous;
    

    【讨论】:

      猜你喜欢
      • 2020-06-15
      • 2020-07-22
      • 2019-01-20
      • 1970-01-01
      • 2014-06-10
      • 1970-01-01
      • 2018-11-16
      相关资源
      最近更新 更多