fix: handle single-node pops in linked-list deques (#1824)

This commit is contained in:
KingCrimsonJY
2026-10-10 19:18:20 +08:00
committed by GitHub
parent bd46fca90d
commit 5b279d48b3
12 changed files with 154 additions and 111 deletions
@@ -76,25 +76,27 @@ class LinkedListDeque {
if (isEmpty())
throw new IndexOutOfBoundsException();
int val;
// 队列长度为 1 时, 头尾指向同一节点
if (queSize == 1) {
val = front.val;
front = null;
rear = null;
}
// 队首出队操作
if (isFront) {
else if (isFront) {
val = front.val; // 暂存头节点值
// 删除头节点
ListNode fNext = front.next;
if (fNext != null) {
fNext.prev = null;
front.next = null;
}
fNext.prev = null;
front.next = null;
front = fNext; // 更新头节点
// 队尾出队操作
} else {
val = rear.val; // 暂存尾节点值
// 删除尾节点
ListNode rPrev = rear.prev;
if (rPrev != null) {
rPrev.next = null;
rear.prev = null;
}
rPrev.next = null;
rear.prev = null;
rear = rPrev; // 更新尾节点
}
queSize--; // 更新队列长度