Skip to content

Instantly share code, notes, and snippets.

@ValentynaGorbachenko
Created October 12, 2016 22:09
Show Gist options
  • Save ValentynaGorbachenko/81f46a7526c780470a8ac3dd37dd167c to your computer and use it in GitHub Desktop.
Save ValentynaGorbachenko/81f46a7526c780470a8ac3dd37dd167c to your computer and use it in GitHub Desktop.
SLL_Node created by ValentynaGorbachenko - https://repl.it/Dq2c/31
function SLL(){
this.head = null;
}
function Node(val){
this.val = val;
this.next = null;
}
Node.prototype.add = function(val){
// check if it is not a null and add it to the next pointer
if(this){
var temp = this.next;
this.next = new Node(val);
this.next.next = temp;
}
return this;
};
SLL.prototype.add = function(val){
// check if sll in not empty
if (this.head){
var curr = this.head;
var prev = null;
// find the element that points to null
while(curr.next){
prev = curr;
curr = curr.next;
}
// console.log("curr ", curr);
if(curr.deleted){
prev.next = new Node(val);
}
curr.next = new Node(val);
} else {
this.head = new Node(val);
}
return this;
};
SLL.prototype.find = function(val){
if(this.head){
var curr = this.head;
while(curr){
if(curr.val == val){
return curr;
}
curr = curr.next;
}
}
return null;
};
Node.prototype.remove = function(){
// check if node is not null
// check if node.next exist
if(this){
if(this.next){
// copy value from the next node to current
// relink to the next after next
this.val = this.next.val;
this.next = this.next.next;
} else {
this.val = undefined;
this.deleted = true;
}
return true;
}
return false;
};
SLL.prototype.remove = function(val){
if(!this.head){ return this; }
var curr = this.head;
// if value is in the first node
if (curr.val == val) {
// console.log("head")
if(this.head.next){
// console.log(curr.next);
this.head = curr.next;
} else {
this.head = null;
}
return this;
}
while(curr.next !== null && curr.next.val !== val){
curr = curr.next;
}
if(curr.next.val == val){
curr.next = curr.next.next;
}
return this;
};
SLL.prototype.detectLoop = function(){
if(!this.head){ return false;}
var slow = this.head,
fast = this.head.next;
while(slow !== fast && fast !== null){
fast = fast.next;
if(fast){
fast = fast.next;
slow = slow.next;
}
}
return slow == fast;
};
SLL.prototype.fixLoop = function(){
if(!this.head){ return this;}
var slow = this.head,
fast = this.head.next;
while(slow !== fast && fast !== null){
fast = fast.next;
if(fast){
fast = fast.next;
slow = slow.next;
}
}
if(slow == fast){
slow = this.head;
while(slow != fast.next){
slow = slow.next;
fast = fast.next;
}
fast.next = null;
console.log("loop fixed, last element in the list is ", fast);
} else {
console.log("there were no loop");
}
return this;
};
Node.prototype.printReverseNode = function(){
if (!this) { return;}
if(this.next) {
this.next.printReverseNode();
console.log("printing in reverse order ",this.val);
} else {
console.log("printing in reverse order ",this.val);
}
};
SLL.prototype.printReverse = function(){
if(!this.head) { return; }
this.head.printReverseNode();
};
SLL.prototype.midPont = function(){
if(!this.head){ return null;}
var slow = this.head,
fast = this.head.next;
while(fast){
fast = fast.next;
if(fast){
slow = slow.next;
fast = fast.next;
}
}
return slow;
};
SLL.prototype.reverse = function(){
if(!this.head){ return this;}
var curr = this.head,
prev = null,
next = null;
while(curr){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
this.head = prev;
return this;
};
SLL.prototype.isPalindrom = function(){
var head = this.head;
function isPalindromNode(node){
var temp;
if(node.next){
temp = isPalindromNode(node.next);
} else {
temp = head;
}
if(temp != null && node.val == temp.val){
if(temp.next == null){
return true; // return temp;
} else {
return temp.next;
}
} else {
return null;
}
}
if(!this.head){
return true;
} else {
return null != isPalindromNode(this.head);
}
};
function mergeSLL(list1, list2){
// merges two lists
}
var list = new SLL();
list.add(1).add(2).add(3);
console.log("list.add(1).add(2).add(3)\n",JSON.stringify(list));
// console.log(list.find(3).remove());
list.remove(3);
console.log("list.remove(3)\n",JSON.stringify(list));
list.add(4).add(5);
console.log("list.add(4).add(5)\n",JSON.stringify(list));
console.log(" list has loop ", list.detectLoop());
list.fixLoop();
list.find(5).next = list.find(2);
console.log("list.find(5).next = list.find(2)");
console.log("list has loop ", list.detectLoop());
list.fixLoop();
console.log("list ufter fix\n",JSON.stringify(list));
list.find(2).add(8).add(7);
console.log("list.find(2).add(8).add(7)\n",JSON.stringify(list));
list.printReverse();
list.reverse();
console.log("list.reverse()\n",JSON.stringify(list));
list.reverse();
console.log("list.reverse()\n",JSON.stringify(list));
console.log("midPoint is ",list.midPont());
var listPpolindrom = new SLL();
listPpolindrom.add('a').add('b').add('a').add('b');
// var node = listPpolindrom.find('a');
console.log("abab is palindrom", listPpolindrom.isPalindrom());
var listPpolindrom2 = new SLL();
listPpolindrom2.add('a').add('b').add('a');
console.log("aba is palindrom", listPpolindrom2.isPalindrom());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment