# freeCodeCamp Challenge Guide: Remove Elements from a Linked List

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-elements-from-a-linked-list/301712>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 5:38pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-elements-from-a-linked-list/301712 "2019-08-03T17:38:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 5:38pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-elements-from-a-linked-list/301712/1 "2019-08-03T17:38:54Z")

</div>

# [Remove Elements from a Linked List](https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/remove-elements-from-a-linked-list)

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```js
> 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 {
> let current = head;
> 
> while(current.next){
> current = current.next;
> }
> current.next = node;
> }
> length++;
> };
> 
> this.remove = function (element) {
> // Only change code below this line
> if (head.element === element) {
> head = head.next;
> return length--;
> }
> let previous = head;
> while (previous) {
> let current = previous.next;
> if (current) { // makes sure we are not at end where current.next would be null
> if (current.element === element) {
> previous.next = current.next;
> return length--;
> }
> }
> previous = current;
> }
> // Only change code above this line
> };
> }
> 
> ```

---

<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:** [January 8, 2025, 3:51pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-remove-elements-from-a-linked-list/301712/2 "2025-01-08T15:51:11Z")

</div>

Closed on January 8, 2025, 3:51pm UTC


