Linked list

by Artem

Babel + JSX

'use struct';

class Node {
  constructor(data) {
    this.next = null;
    this.data = data;
  };
}

class LinkedList {
  constructor() {
    this.head = null;
  }

  append(data) {
    if (this.head === null) {
      this.head = new Node(data);
      return this;
    }

    let current = this.head;

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

    current.next = new Node(data);
    return this;
  }

  prepend(data) {
    const newHead = new Node(data);
    newHead.next = this.head;
    this.head = newHead;

    return this;
  }

  deleteWithValue(data) {
    if (this.head.data === data) {
      this.head = this.head.next;
      return this;
    }

    let current = this.head;

    while (current.next !== null) {
      if (current.next.data === data) {
        current.next = current.next.next;
        return this;
      }
      current = current.next;
    }

    return this;

  }
}

var ll = new LinkedList();
ll.append('Hello');
ll.append('World');
ll.prepend('World World');
ll.deleteWithValue('World');

console.dir(ll);