# Linked list operations

**URL:** <https://forum.freecodecamp.org/t/linked-list-operations/787342>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [April 29, 2026, 3:36pm UTC](https://forum.freecodecamp.org/t/linked-list-operations/787342 "2026-04-29T15:36:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harshaallakonda1998](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/harshaallakonda1998/32/413471_2.png) [@harshaallakonda1998](https://forum.freecodecamp.org/u/harshaallakonda1998)\
**Post date:** [April 29, 2026, 3:36pm UTC](https://forum.freecodecamp.org/t/linked-list-operations/787342/1 "2026-04-29T15:36:26Z")

</div>

```auto
function initList() {
return {
head: null,
length: 0
};
}

function isEmpty(list) {
return list.length === 0;
}

function add(list, element) {
const node = { element, next: null };

if (isEmpty(list)) {
list.head = node;
} else {
let current = list.head;
while (current.next !== null) {
current = current.next;
}
current.next = node;
}

list.length++;
}

function remove(list, element) {
let previous = null;
let current = list.head;

while (current !== null && current.element !== element) {
previous = current;
current = current.next;
}

if (current === null) return;

if (previous !== null) {
previous.next = current.next;
} else {
list.head = current.next;
}

list.length–;
}

function contains(list, element) {
let current = list.head;

while (current !== null) {
if (current.element === element) {
return true;
}
current = current.next;
}
return false;
}

function getAt(list, index) {
let current = list.head;
let i = 0;

while (current) {
if (i === index) {
return current
}
i++;
current = current.next;
}
return undefined;
}

```

---

<div class="post-metadata">

**Author:** ![dhess](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dhess/32/406235_2.png) [@dhess](https://forum.freecodecamp.org/u/dhess)\
**Post date:** [April 29, 2026, 4:23pm UTC](https://forum.freecodecamp.org/t/linked-list-operations/787342/2 "2026-04-29T16:23:26Z")

</div>

Welcome to the forum @harshaallakonda1998!

Please **Tell us what’s happening** in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

* * *

I’ve edited your post to improve the readability of the code. When you enter a code block into a forum post, please precede it with three backticks to make it easier to read and test.

You can also use the “preformatted text” tool in the editor (`</>`) to add the backticks.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (').

* * *

Please provide a link to the challenge you are working on.

Happy coding!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [May 27, 2026, 4:24pm UTC](https://forum.freecodecamp.org/t/linked-list-operations/787342/3 "2026-05-27T16:24:27Z")

</div>

Closed on May 27, 2026, 4:24pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
