removeLast metodu
Şöyle yaparız.
Şöyle yaparız.
function removeLast() {
  // Edge case when there is only 1 element in the queue.
  if (head == tail) {                   // O(1)
    value = head.value                // O(1)
    head = null                       // O(1)
    tail = null                       // O(1)
    return value                      // O(1)
  }
  // Searching for the new tail.
  newTail = head                        // O(1)
  while (newTail.next != tail) {        // O(n)
    newTail = newTail.next            // O(1)
  }
  value = tail.value                    // O(1)
  newTail.next = null                   // O(1)
  tail = newTail                        // O(1)
  return tail                           // O(1)    
} 
 
Hiç yorum yok:
Yorum Gönder