# Suggest Solution for Search within a Linked List

Continuing the discussion from freeCodeCamp Challenge Guide: Search within a Linked List:

Suggested Solution

Solution 3
``````function LinkedList() {
var length = 0;
var head = null;

var Node = function(element){
this.element = element;
this.next = null;
};

this.size = function() {
return length;
};

this.head = function(){
return head;
};

this.add = function(element){
var node = new Node(element);
if(head === null){
head = node;
} else {
var currentNode = head;

while(currentNode.next){
currentNode = currentNode.next;
}

currentNode.next = node;
}

length++;
};

this.remove = function(element){
var currentNode = head;
var previousNode;
if(currentNode.element === element){
head = currentNode.next;
} else {
while(currentNode.element !== element) {
previousNode = currentNode;
currentNode = currentNode.next;
}

previousNode.next = currentNode.next;
}

length --;
};

this.indexOf = (el)=>{
let index = 0;
let found = false;
let currentNode = head;
while(currentNode.next){
if(currentNode.element == el){
found = true;
break;
}
index++;
currentNode = currentNode.next;
}
return found ? index : -1;
}

this.elementAt = (index)=>{
let currentNode = head;
let found;
let count = 0;
while(count <= index && index < this.size()){
if(count == index){
found = currentNode.element;
}
count++;
currentNode = currentNode.next;
}
return found;
}
this.isEmpty = ()=>{
return !Boolean(this.size());
}
}
``````
``````this.elementAt = function(i) {

if(i > length || i < 0) return ;

let temp = head;

while(i > 0 && temp != null) {

i--;

temp = temp.next;

}

return temp.element

}
``````

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please donâ€™t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with `[spoiler]` and `[/spoiler]` tags on the line above and below your solution code.