Data Structures & Algorithms in JavaScript(Single Linked List) Part 2
Tags: javascript, datastructures, beginners, linkedlist
Published: 2019-12-23 10:45:10

Hello Everyone, this is part 5.2 in the series of blogs about data structures and algorithms in JavaScript, In the previous blog,  I had covered the linked list's [*push*](https://dev.to/swarup260/data-structures-algorithms-in-javascript-single-linked-list-part-1-3ghg#push), [*insert*](https://dev.to/swarup260/data-structures-algorithms-in-javascript-single-linked-list-part-1-3ghg#insert) and [*getElementAt*](https://dev.to/swarup260/data-structures-algorithms-in-javascript-single-linked-list-part-1-3ghg#GetElementAt) methods. In this, I cover the remaining methods *removeAt*, *remove*, and *indexOf*.



##Implementation of linked list in Javascript 

###IndexOf
This method will return the *index* of the given element if exists else return __-1__ __({4})__ . To find the *index* of the element, we will start with the head element __({1})__  and loop until the element is found __({2})__  and returns its index __({3})__ .

```javascript

indexOf(element) {
        let current = this.head; //1
        for (let index = 0; index < this.count && current != null; index++) {
            if (current.element == element) { //2
                return index;
            }
            current = current.next; //3
        }
        return -1; //4
    }

```
###RemoveAt

Remove an element at the specified index, we first check if the linked list is *empty* else return undefined __({1})__ ,After that we valid the index's out of bound error, by check is the index, greater than zero and less than *count*.
* We want to remove the first element i.e index equal to zero __({2})__, shift the head to the next node. We will refer to the first element of the list using the current variable. If we assign head to current's next, we will remove the first element __({3})__.

![Remove head node from linked list](https://thepracticaldev.s3.amazonaws.com/i/ux4jidvxkcyjaxuwnkko.png)

*  We want to remove the last element or an element from the linked list, we will use *getElementAt* method to get the one previous element using index -1 __({4})__, current will be previous's next __({5})__. So, to remove the current node, all we have do is to link the __previous.next__ to __current.next__ __({6})__. 

![Remove any node from linked list](https://thepracticaldev.s3.amazonaws.com/i/2hgu2y8ddygea8o45413.png)

```javascript



removeAt(index) {
        if (this.isEmpty()) {
            return undefined; //1
        }        
        if (index >= 0 && index < this.count) {
            
            let current = this.head
            if (index == 0) { // 2
                this.head = current.next;  //3 
            } else {
                let previous = this.getElementAt(index - 1);  //4               
                current = previous.next; //5
                previous.next = current.next; //6
            }
            this.count--;
            return current.element;
        }
        return undefined;
    }


```


###Remove

To remove an element, we check if the linked list is not empty.
If not then get the index of the element using the *indexOf* method if the index is __-1__ then the element doesn't exist else use the  index and remove the element from the linked list using *removeAt* method.


```javascript

remove(element) {
        if (this.isEmpty()) {
            return undefined;
        }
        let index = this.indexOf(element);
        if (index != -1) {
            this.removeAt(index);
        }
        return undefined;
    }



```

you get the full source [here](https://github.com/swarup260/Learning_Algorithms/blob/master/data_structure/LinkedList.js)


###Conclusion : 



| Methods       | Complexity     |
| ------------- |:-------------: | 
| indexOf       | O(n)          | 
| remove head element        | O(1)          |
| remove any element(removeAt)         | O(n)          |     

So, stay tuned for the next blog, in which I will cover another DS [__Doubly Linked List__](https://dev.to/swarup260/data-structures-algorithms-in-javascript-doubly-linked-list-4op2).