-1

I tried to implement a Linked list by myself and faced the question: "How I can get Node for O(1) complexity?"

Are there opportunities (or good practices) to improve "get" method? Do I need to add another data structure into Linked List? I read in some articles about using Hash Table into Linked List. Is it normal?

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

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

  // O(1)
  addToTail(value) {
    let newNode = new Node(value);

    if (this.head === null) {
      this.head = newNode;
      this.tail = newNode;
    } else {
      this.tail.next = newNode;
      this.tail = newNode;
    }
  }

  // O(n)
  get(value) {
    let cur = this.head;
    while (cur && cur.value !== value) {
      cur = cur.next;
    }

    return cur;
  }

  // O(n)
  remove(value) {
    let cur = this.head;
    let prev = null;
    while (cur && cur.value !== value) {
      prev = cur;
      cur = cur.next;
    }

    if (cur) {
      // First Node
      if (this.head === cur) {
        this.head = cur.next;
        if (this.head === null) {
          this.tail = null;
        }
      } else {
        // Not first Node
        prev.next = cur.next;

        if (cur.next === null) {
          this.tail = prev;
        }
      }

      return true;
    }

    return false;
  }

  print() {
    let cur = this.head;
    while (cur !== null) {
      console.log(cur.value);
      cur = cur.next;
    }
  }
}

const cars = ['Audio', 'BMW', 'Mazda', 'Toyota'];
const list = new LinkedList();
for (let i = 0; i < cars.length; i++) {
  list.addToTail(cars[i])
}

list.remove('Audio')
list.addToTail('Kia')
list.addToTail('Lexus')
console.log(list.get('Mazda'));
Isa
  • 418
  • 5
  • 13
  • 2
    *O(n)* performance for getting an element of a linked list is expected. – Pointy Jun 02 '20 at 18:07
  • 1
    Vanilla linked list is O(n) for getting an element. Check it out-> https://stackoverflow.com/questions/40718116/complexity-of-different-operations-on-different-data-structures-according-to-the/40718359 – Pedro Mutter Jun 02 '20 at 18:09

1 Answers1

1

...and faced the question: "How I can get Node for O(1) complexity?"

You can't. A linked list always requires some level of scanning. A pure singly-linked list is O(n), IIRC.

I read in some articles about using Hash Table into Linked List. Is it normal?

It's not uncommon to have a map structure that does a hash of the key to find the right "bucket," where a "bucket" is a linked list or similar linearly-searched container for all of the entries whose keys hash the same. Basically, get is then:

  • Do the hash for the key
  • Look up the bucket by hash
  • Search in the bucket for the key

At that point, though, the overall thing isn't a linked list anymore, it's more of a map. Even a map backed by hash tables isn't O(1), though it'll be better than O(n).

(I realize you're doing this for learning purposes, but I have to point to JavaScript's native Map, which does use hashing or (quoting the spec) "...other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.")

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875