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

// 循环链
export class CircularLinkedList {
  constructor() {
    this.head = null
    this.tail = null
  }

  // 向后追加
  append(value) {
    const newNode = new Node(value)
    if (!this.head) {
      this.head = newNode
      this.tail = newNode
      this.tail.next = this.head
    } else {
      this.tail.next = newNode
      this.tail = newNode
      this.tail.next = this.head
    }
  }

  // 获取链表长度
  getLength() {
    if (!this.head) return 0
    let length = 0
    let currentNode = this.head
    do {
      length++
      currentNode = currentNode.next
    } while (currentNode !== this.head)
    return length
  }
}
