JavaScript Data Structures: Singly Linked List: Set
Tags: beginners, tutorial, javascript, webdev
Published: 2019-11-28 09:48:02

---
title: "JavaScript Data Structures: Singly Linked List: Set"
description: "JavaScript Data Structures: Singly Linked List: Set"
published: true
tags: ["Beginners", "Tutorial", "JavaScript", "Webdev"]
series: JavaScript Data Structures
---

## Intro

[Last time](https://dev.to/miku86/javascript-data-structures-singly-linked-list-get-2503), we learned how to get a specific node by its index.

Today, we learn how to update / set a specific node.

---

## Current Code

We start with the code [after we added `get()`](https://dev.to/miku86/javascript-data-structures-singly-linked-list-get-2503), because we can use our `get` method to get the node we want to change.

```js
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class SinglyLinkedList {
  constructor() {
    this.length = 0;
    this.head = null;
    this.tail = null;
  }

  push(value) {
    const newNode = new Node(value);
    if (this.length > 0) {
      this.tail.next = newNode;
    } else {
      this.head = newNode;
    }
    this.tail = newNode;
    this.length += 1;
    return newNode;
  }

  get(index) {
    if (index < 0 || index >= this.length) {
      return null;
    } else {
      let currentNode = this.head;
      let count = 0;

      while (count < index) {
        currentNode = currentNode.next;
        count += 1;
      }

      return currentNode;
    }
  }
}
```

---

## Thoughts

First, we should think about the constraints and possibilities:

Because we will use our `get` method to get the node, this is straight-forward:

- get the node at the desired index
- if the node does exist, set its value to the desired new value and return it
- if the node does not exist, return null

---

## Implementation (Short version, DRY)

```js
class Node {
  constructor(value) {
    this.value = value;
    this.next = null;
  }
}

class SinglyLinkedList {
  constructor() {
    this.length = 0;
    this.head = null;
    this.tail = null;
  }

  push(value) {
    const newNode = new Node(value);
    if (this.length > 0) {
      this.tail.next = newNode;
    } else {
      this.head = newNode;
    }
    this.tail = newNode;
    this.length += 1;
    return newNode;
  }

  get(index) {
    if (index < 0 || index >= this.length) {
      return null;
    } else {
      let currentNode = this.head;
      let count = 0;

      while (count < index) {
        currentNode = currentNode.next;
        count += 1;
      }

      return currentNode;
    }
  }

  set(index, value) {
    // get the node at the desired index
    const currentNode = this.get(index);
    // if the node does exist
    if (currentNode) {
      // set its value to the desired new value
      currentNode.value = value;
      // and return it
      return currentNode;
    } else {
      // if the node does not exist, return null
      return null;
    }
  }
}
```

---

## Result

Let's have a look how to use the Singly Linked List's `set` method and its results.

```js
const newSLL = new SinglyLinkedList();
// show List, should be empty
console.log(newSLL);
// SinglyLinkedList { length: 0, head: null, tail: null }

// change a node that does not exist => return null and the empty List
console.log(newSLL.set(0, "new 0"));
// null
console.log(newSLL);
// SinglyLinkedList { length: 0, head: null, tail: null }

// add two nodes and updated both => return updated nodes and show updated List
newSLL.push("0");
newSLL.push("1");
console.log(newSLL.set(0, "new 0")); // Node { value: 'new 0', next: Node { value: '1', next: null } }
console.log(newSLL.set(1, "new 1")); // Node { value: 'new 1', next: null }
console.log(newSLL);
// SinglyLinkedList {
//   length: 2,
//   head: Node { value: 'new 0', next: Node { value: 'new 1', next: null } },
//   tail: Node { value: 'new 1', next: null }
// }
```

---

### Next Part

We will implement how to insert a new node at a specific index. If you want to be notified, [subscribe](https://miku86.com/) :)