从单链表中删除节点
Remove node from 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 个节点,则此函数仅 returns 第 4-10 个节点,其中第 5 个节点被删除。但是,我希望它成为 return 1-10th,其中 5th 被删除。我做错了什么以及如何检索仅删除第 5 个节点的列表?
还在
您在代码中犯了一个小错误
1) while 循环应该 运行 直到 < index-1 因为你从 0
开始计数
2) 你没有做 this._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));
我正在 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 个节点,则此函数仅 returns 第 4-10 个节点,其中第 5 个节点被删除。但是,我希望它成为 return 1-10th,其中 5th 被删除。我做错了什么以及如何检索仅删除第 5 个节点的列表?
还在 您在代码中犯了一个小错误
1) while 循环应该 运行 直到 < index-1 因为你从 0
开始计数2) 你没有做 this._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));