javascript - Remove node from Singly Linked List -


i'm implementing remove function ll in javascript.

here's function:

//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 first node        if(!currentnode){          this.head = node;          this._length++;          return;        }          //iterate on 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 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)));

problem: if had 10 nodes in list , called function remove 5th node, function returns 4th-10th nodes 5th removed. however, i'd expect return 1-10th 5th removed. doing wrong , how can retrieve list 5th node removed?

still made small mistake in code

1) while loop should run till < index-1 starting count @ 0

2) not doing this._length-- after deleting node other first

3) json.stringify starting printing head element, when deleting node returning previous node getting wrong node list

the corrected code here

//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 first node       if(!currentnode){         this.head = node;         this._length++;         return;       }        //iterate on 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));     

Comments