# Suggest Solution for Search within a Linked List

**URL:** <https://forum.freecodecamp.org/t/suggest-solution-for-search-within-a-linked-list/435470>\
**Category:** General\
**Created:** [December 14, 2020, 11:48pm UTC](https://forum.freecodecamp.org/t/suggest-solution-for-search-within-a-linked-list/435470 "2020-12-14T23:48:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![thonker86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/thonker86/32/189576_2.png) [@thonker86](https://forum.freecodecamp.org/u/thonker86)\
**Post date:** [December 14, 2020, 11:48pm UTC](https://forum.freecodecamp.org/t/suggest-solution-for-search-within-a-linked-list/435470/1 "2020-12-14T23:48:08Z")

</div>

Continuing the discussion from [freeCodeCamp Challenge Guide: Search within a Linked List](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-search-within-a-linked-list/301715):

**Suggested Solution**

> **Solution 3**
>
> ```auto
> 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());
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![nightroit](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nightroit/32/196994_2.png) [@nightroit](https://forum.freecodecamp.org/u/nightroit)\
**Post date:** [January 6, 2021, 3:18pm UTC](https://forum.freecodecamp.org/t/suggest-solution-for-search-within-a-linked-list/435470/2 "2021-01-06T15:18:43Z")

</div>

```auto
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

  }

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [January 6, 2021, 3:21pm UTC](https://forum.freecodecamp.org/t/suggest-solution-for-search-within-a-linked-list/435470/3 "2021-01-06T15:21:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:36pm UTC](https://forum.freecodecamp.org/t/suggest-solution-for-search-within-a-linked-list/435470/4 "2021-06-01T18:36:19Z")

</div>

Closed on June 1, 2021, 6:36pm UTC


