peterschussheim
7/27/2017 - 7:43 PM

Linked List Class

Linked List Class

// WIP
class ListNode {
  constructor(val) {
    this.val = val
    this.next = null
  }
}

class LinkedList {
  constructor(head) {
    this.head = head
    this.length = 0
  }
  add(val) {
    const nodeToAdd = new ListNode(val)
    let nodeToCheck = this.head

    if (!nodeToCheck) {
      this.head = nodeToAdd
      this.length++
      return nodeToAdd
    }

    while (nodeToCheck.next) {
      nodeToCheck = nodeToCheck.next
    }

    nodeToCheck.next = nodeToAdd
    this.length++
    return nodeToAdd
  }
}