从单链接列表中删除节点

时间:2016-09-02 02:58:31

标签: javascript singly-linked-list

我正在Javascript中实现LL的删除功能。

这是我的功能:

//Define Node obj
function Node(data){
  this.data = data;
  this.next = null;
}

//Define SinglyList obj
function SinglyList(){
  this._length = 0;
  this.head = null;
}

SinglyList.prototype.add = function(val){
  var node = new Node(val),
      currentNode = this.head;

      //If empty, build as first node
      if(!currentNode){
        this.head = node;
        this._length++;
        return;
      }

      //iterate over until end of list
      while(currentNode.next){
        currentNode = currentNode.next;
      }

      //add/append new node
      currentNode.next = node;
      this._length++;

      return node;
};

SinglyList.prototype.remove = function(index){
  var currentNode = this.head, count=0, previous;
  //if list is empty, exit out
  if(this._length===0) return;

  //Check if first node
  if(index===0){
      this.head = currentNode.next;
      this._length--;
  }else{

      while(count<index){
        previous = currentNode;
        currentNode = currentNode.next;
        count++;
      }//end while

      previous.next = currentNode.next;

      return previous;
  }// end if

};

var singlyList = new SinglyList();

singlyList.add(1);
singlyList.add(2);
singlyList.add(3);
singlyList.add(4);
singlyList.add(5);
singlyList.add(6);
singlyList.add(7);
singlyList.add(8);
singlyList.add(9);
singlyList.add(10);

console.log(JSON.stringify(singlyList));
console.log('Remove:\n'+JSON.stringify(singlyList.remove(5)));

问题:如果我的列表中有10个节点并且调用此函数来删除第5个节点,则此函数仅返回第5个节点,其中第5个节点被删除。但是,我希望它会在第5位被删除,而第5位被删除。我做错了什么以及如何检索仅删除第5个节点的列表?

1 个答案:

答案 0 :(得分:2)

还是 你在代码中犯了小错误

1)while循环应该运行到&lt; index-1,因为你开始计数为0

2)删除第一个

以外的节点后,你没有这样做._length--

3)JSON.stringify使用head元素开始打印,当你删除节点时,你正在返回上一个节点,所以你得到错误的节点列表

更正的代码在这里

//Define Node obj
function Node(data){
  this.data = data;
  this.next = null;
}

//Define SinglyList obj
function SinglyList(){
  this._length = 0;
  this.head = null;
}

SinglyList.prototype.add = function(val){
  var node = new Node(val),
      currentNode = this.head;

      //If empty, build as first node
      if(!currentNode){
        this.head = node;
        this._length++;
        return;
      }

      //iterate over until end of list
      while(currentNode.next){
        currentNode = currentNode.next;
      }

      //add/append new node
      currentNode.next = node;
      this._length++;

      return node;
};

SinglyList.prototype.remove = function(index){
  var currentNode = this.head, count=0, previous;
  //if empty, exit out
  if(this._length===0) return;

  //Check against first node
  if(index===0){
      this.head = currentNode.next;
      this._length--;
  }else{

      while(count<index-1){
        previous = currentNode;
        currentNode = currentNode.next;
        count++;
      }//end while

      previous.next = currentNode.next;
      this._length--;

      return this.head;
  }// end if

};

var singlyList = new SinglyList();

singlyList.add(1);
singlyList.add(2);
singlyList.add(3);
singlyList.add(4);
singlyList.add(5);
singlyList.add(6);
singlyList.add(7);
singlyList.add(8);
singlyList.add(9);
singlyList.add(10);

document.write(JSON.stringify(singlyList));
singlyList.remove(5)
document.write('After removing 5 :\n'+JSON.stringify(singlyList));