Skip to content

Bug Report for queue #4207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Sedose opened this issue Jun 1, 2025 · 0 comments
Open

Bug Report for queue #4207

Sedose opened this issue Jun 1, 2025 · 0 comments

Comments

@Sedose
Copy link

Sedose commented Jun 1, 2025

Bug Report for https://neetcode.io/problems/queue

Describe the bug below and include any steps to reproduce the bug or screenshots if possible.

import java.util.OptionalInt;

public class Deque {
  private static class Node {
    int value;
    Node previous;
    Node next;

    Node(int value) {
      this.value = value;
    }
  }

  private Node head;
  private Node tail;

  public boolean isEmpty() {
    return head == null;
  }

  public void append(int value) {
    Node newTail = new Node(value);
    if (isEmpty()) {
      head = newTail;
      tail = newTail;
      return;
    }
    tail.next = newTail;
    newTail.previous = tail;
    tail = newTail;
  }

  public void appendLeft(int value) {
    Node newHead = new Node(value);
    if (isEmpty()) {
      head = newHead;
      tail = newHead;
      return;
    }
    head.previous = newHead;
    newHead.next = head;
    head = newHead;
  }

  public OptionalInt pop() {
    if (isEmpty()) {
      return OptionalInt.empty();
    }
    int removedValue = tail.value;
    tail = tail.previous;
    if (tail == null) {
      head = null;
    } else {
      tail.next = null;
    }
    return OptionalInt.of(removedValue);
  }

  public OptionalInt popLeft() {
    if (isEmpty()) {
      return OptionalInt.empty();
    }
    int removedValue = head.value;
    head = head.next;
    if (head == null) {
      tail = null;
    } else {
      head.previous = null;
    }
    return OptionalInt.of(removedValue);
  }
}

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant