function ListNode(val) {
this.val = val;
this.next = null;
}
function LinkList() {
this.length = 0;
this.head = null;
}
LinkList.prototype.getElementAt = function(index) {
if(index < 0 || this.length) return null;
let cur = this.head;
while(index--) {
cur = cur.next;
}
return cur;
};
LinkList.prototype.find = function(val) {
let cur = this.head;
while(cur) {
if(cur.val === val) return cur;
cur = cur.next
}
return null;
};
LinkList.prototype.append = function(val) {
let node = new ListNode(val);
if(!this.head) {
this.head = node;
}else{
let cur = this.getElementAt(this.length -1);
cur.next = node;
}
this.length++;
};
LinkList.prototype.insert = function(index, val) {
if(index < 0 || index > this.length) return false;
let node = new ListNode(val);
if(index === 0) {
node.next = this.head;
this.head = node;
}else{
let pre = this.getElementAt(index-1);
node.next = pre.next;
pre.next = node;
}
this.length++;
return true
};
LinkList.prototype.removeAt = function(index) {
if(index < 0 || index >= this.length) return null;
let cur = this.head;
if(index === 0) {
this.head = cur.next;
}else{
let pre = this.getElementAt(index-1);
cur = pre.next;
pre.next = cur.next;
}
this.length--;
return cur.val;
};
LinkList.prototype.indexOf = function(val) {
let cur = this.head;
for(let i=0;i<this.length;i++) {
if(cur.val === val) return i;
cur = cur.next;
}
return -1;
};
LinkList.prototype.remove = function(val) {
let index = this.indexOf(val);
return this.removeAt(index);
};
LinkList.prototype.isEmpty = function(val) {
return !this.length;
};
LinkList.prototype.size = function(val) {
return this.length;
};
LinkList.prototype.getHead = function(val) {
return this.head;
};
LinkList.prototype.clear = function(val) {
this.head = null;
this.length = 0;
};
LinkList.prototype.join = function(val) {
let cur = this.head;
let str = '';
while(cur) {
str += cur.val;
if(cur.next) str += string;
cur = cur.next;
}
return str;
};
console