DEV Community

miku86
miku86

Posted on

JavaScript Data Structures: Singly Linked List: Set

Intro

Last time, 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(), because we can use our get method to get the node we want to change.

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;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

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)

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;
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Result

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

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 }
// }
Enter fullscreen mode Exit fullscreen mode

Next Part

We will implement how to insert a new node at a specific index. If you want to be notified, subscribe :)

Top comments (0)