mirror of
https://github.com/krahets/hello-algo.git
synced 2026-10-21 21:45:19 +08:00
fix: handle single-node pops in linked-list deques (#1824)
This commit is contained in:
@@ -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--; // 更新队列长度
|
||||
|
||||
Reference in New Issue
Block a user