Skip to content

fix: handle single node case in pop operation for linked list deque#1824

Open
KingCrimsonJY wants to merge 1 commit into
krahets:mainfrom
KingCrimsonJY:main
Open

fix: handle single node case in pop operation for linked list deque#1824
KingCrimsonJY wants to merge 1 commit into
krahets:mainfrom
KingCrimsonJY:main

Conversation

@KingCrimsonJY
Copy link
Copy Markdown

@KingCrimsonJY KingCrimsonJY commented Nov 5, 2025

If this pull request (PR) pertains to Chinese-to-English translation, please confirm that you have read the contribution guidelines and complete the checklist below:

  • This PR represents the translation of a single, complete document, or contains only bug fixes.
  • The translation accurately conveys the original meaning and intent of the Chinese version. If deviations exist, I have provided explanatory comments to clarify the reasons.

If this pull request (PR) is associated with coding or code transpilation, please attach the relevant console outputs to the PR and complete the following checklist:

  • [ x ] I have thoroughly reviewed the code, focusing on its formatting, comments, indentation, and file headers.
  • [ x ] I have confirmed that the code execution outputs are consistent with those produced by the reference code (Python or Java).
  • [ x ] The code is designed to be compatible on standard operating systems, including Windows, macOS, and Ubuntu.

@diffray-bot
Copy link
Copy Markdown

Changes Summary

This PR fixes a critical null pointer dereference bug in the LinkedListDeque pop() operation when the deque contains a single element. The fix adds special handling for the single-element case across 12 programming languages, ensuring both front and rear pointers are safely nullified before attempting to access next/previous node references.

Type: bugfix

Components Affected: LinkedListDeque.pop() method, Code examples for data structures chapter

Files Changed
File Summary Change Impact
...es/c/chapter_stack_and_queue/linkedlist_deque.c Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴
...pp/chapter_stack_and_queue/linkedlist_deque.cpp Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴
...arp/chapter_stack_and_queue/linkedlist_deque.cs Added single-element case handling and improved null-safety assertions in pop() method ✏️ 🔴
...t/chapter_stack_and_queue/linkedlist_deque.dart Added single-element case handling and simplified pointer assignments in pop() method ✏️ 🔴
...a/chapter_stack_and_queue/linkedlist_deque.java Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴
...ipt/chapter_stack_and_queue/linkedlist_deque.js Added single-element case handling, fixed comment error, and simplified pointer assignments in pop methods ✏️ 🔴
...lin/chapter_stack_and_queue/linkedlist_deque.kt Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴
...hon/chapter_stack_and_queue/linkedlist_deque.py Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴
...uby/chapter_stack_and_queue/linkedlist_deque.rb Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴
.../chapter_stack_and_queue/linkedlist_deque.swift Added single-element case handling and removed redundant nil checks in pop() method ✏️ 🔴
...ipt/chapter_stack_and_queue/linkedlist_deque.ts Added single-element case handling, fixed comment error, and improved null-safety in pop methods ✏️ 🔴
...ig/chapter_stack_and_queue/linkedlist_deque.zig Added single-element case handling and removed redundant null checks in pop() method ✏️ 🔴

Risk Areas: Edge case handling: Single-element deque operations must work correctly across all languages, Language-specific null/nil handling semantics must be respected (e.g., Kotlin!!, Swift!, C# null-coalescing), Pointer/reference management in low-level languages (C, C++) after nullification, Consistency verification: All 12 language implementations should behave identically

Suggestions
  • Verify test coverage includes single-element deque pop operations in the test suite
  • Consider adding edge-case test cases for all language implementations to prevent future regressions
  • Ensure the fix is consistent with the push() method's single-element handling logic
  • Review similar deque/queue implementations in the repository to apply the same pattern if needed

Full review in progress... | Powered by diffray

this.front = temp; // 更新头节点
this.queSize--;
return value;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 HIGH - Return type mismatch: returning null for number return type
Agent: typescript

Category: quality

Description:
The peekLast() method is typed to return number but returns null when queue is empty. This violates the type contract.

Suggestion:
Change return type to number | null: peekLast(): number | null { ... }

Confidence: 95%
Rule: ts_non_null_assertion
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

this.front = temp; // 更新头节点
this.queSize--;
return value;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 HIGH - Return type mismatch: returning null for number return type
Agent: typescript

Category: quality

Description:
The peekFirst() method is typed to return number but returns null when queue is empty. This violates the type contract.

Suggestion:
Change return type to number | null: peekFirst(): number | null { ... }

Confidence: 95%
Rule: ts_non_null_assertion
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

Comment on lines 66 to 68
if (this.queSize === 0) {
return null;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 HIGH - Return type mismatch: returning null for number return type
Agent: typescript

Category: quality

Description:
The popLast() method is typed to return number but returns null when the queue is empty. This violates the type contract and allows null to be assigned to number variables.

Suggestion:
Change return type to number | null: popLast(): number | null { ... }

Confidence: 95%
Rule: ts_non_null_assertion
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

Comment on lines 88 to 90
if (this.queSize === 0) {
return null;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 HIGH - Return type mismatch: returning null for number return type
Agent: typescript

Category: quality

Description:
The popFirst() method is typed to return number but returns null when the queue is empty. This violates the type contract and allows null to be assigned to number variables.

Suggestion:
Change return type to number | null: popFirst(): number | null { ... }

Confidence: 95%
Rule: ts_non_null_assertion
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

Comment on lines 88 to +89
ListNode? fNext = front?.next;
if (fNext != null) {
fNext.prev = null;
front!.next = null;
}
fNext!.prev = null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM - Null-forgiving operator usage without explicit validation
Agent: csharp

Category: bug

Description:
fNext is assigned from front?.next which could theoretically be null. The null-forgiving operator ! suppresses the compiler warning. While the deque invariant (queSize > 1 means front.next exists) protects this in practice, explicit validation would be more defensive.

Suggestion:
Consider adding explicit null check or assertion: if (fNext == null) throw new InvalidOperationException("Deque invariant violated");

Confidence: 65%
Rule: cs_add_null_checks_to_prevent_nullreference
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

Comment on lines 97 to +98
ListNode? rPrev = rear?.prev;
if (rPrev != null) {
rPrev.next = null;
rear!.prev = null;
}
rPrev!.next = null;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM - Null-forgiving operator usage without explicit validation
Agent: csharp

Category: bug

Description:
rPrev is assigned from rear?.prev which could theoretically be null. The null-forgiving operator ! suppresses the compiler warning. While the deque invariant protects this in practice, explicit validation would be more defensive.

Suggestion:
Consider adding explicit null check or assertion: if (rPrev == null) throw new InvalidOperationException("Deque invariant violated");

Confidence: 65%
Rule: cs_add_null_checks_to_prevent_nullreference
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

Comment on lines 80 to +81
val fNext = front!!.next
if (fNext != null) {
fNext.prev = null
front!!.next = null
}
fNext!!.prev = null
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM - Unsafe not-null assertion on linked list node reference
Agent: kotlin

Category: bug

Description:
Line 80-81 uses !! to assert front!!.next and fNext!! are non-null. While the deque invariant (queSize > 1) should guarantee this, using requireNotNull with descriptive message would provide better error messages if invariant is violated.

Suggestion:
Use requireNotNull(front?.next) { "Front node must have next when size > 1" } for better error messages

Confidence: 65%
Rule: bug_null_safety_kotlin
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

Comment on lines 88 to +89
val rPrev = rear!!.prev
if (rPrev != null) {
rPrev.next = null
rear!!.prev = null
}
rPrev!!.next = null
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM - Unsafe not-null assertion on linked list node reference
Agent: kotlin

Category: bug

Description:
Line 88-89 uses !! to assert rear!!.prev and rPrev!! are non-null. While the deque invariant protects this, using requireNotNull would provide better error messages.

Suggestion:
Use requireNotNull(rear?.prev) { "Rear node must have prev when size > 1" } for better error messages

Confidence: 65%
Rule: bug_null_safety_kotlin
Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

@diffray-bot
Copy link
Copy Markdown

Review Summary

Free public review - Want AI code reviews on your PRs? Check out diffray.ai

Validated 24 issues: 8 kept (TypeScript type mismatches are real bugs, Kotlin null safety concerns have merit), 16 filtered (mostly defensive programming concerns where deque invariants protect against null access, and false positives for short-circuit evaluation)

Issues Found: 8

💬 See 8 individual line comment(s) for details.

📊 3 unique issue type(s) across 8 location(s)

📋 Full issue list (click to expand)

🟠 HIGH - Return type mismatch: returning null for number return type (4 occurrences)

Agent: typescript

Category: quality

📍 View all locations
File Description Suggestion Confidence
codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts:106 The peekLast() method is typed to return number but returns null when queue is empty. This violates ... Change return type to number | null: peekLast(): number | null { ... } 95%
codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts:106 The peekFirst() method is typed to return number but returns null when queue is empty. This violates... Change return type to number | null: peekFirst(): number | null { ... } 95%
codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts:66-68 The popLast() method is typed to return number but returns null when the queue is empty. This violat... Change return type to number | null: popLast(): number | null { ... } 95%
codes/typescript/chapter_stack_and_queue/linkedlist_deque.ts:88-90 The popFirst() method is typed to return number but returns null when the queue is empty. This viola... Change return type to number | null: popFirst(): number | null { ... } 95%

Rule: ts_non_null_assertion


🟡 MEDIUM - Null-forgiving operator usage without explicit validation (2 occurrences)

Agent: csharp

Category: bug

📍 View all locations
File Description Suggestion Confidence
codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs:88-89 fNext is assigned from front?.next which could theoretically be null. The null-forgiving operator ! ... Consider adding explicit null check or assertion: if (fNext == null) throw new InvalidOperationExcep... 65%
codes/csharp/chapter_stack_and_queue/linkedlist_deque.cs:97-98 rPrev is assigned from rear?.prev which could theoretically be null. The null-forgiving operator ! s... Consider adding explicit null check or assertion: if (rPrev == null) throw new InvalidOperationExcep... 65%

Rule: cs_add_null_checks_to_prevent_nullreference


🟡 MEDIUM - Unsafe not-null assertion on linked list node reference (2 occurrences)

Agent: kotlin

Category: bug

📍 View all locations
File Description Suggestion Confidence
codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt:80-81 Line 80-81 uses !! to assert front!!.next and fNext!! are non-null. While the deque invariant (queSi... Use requireNotNull(front?.next) { "Front node must have next when size > 1" } for better error messa... 65%
codes/kotlin/chapter_stack_and_queue/linkedlist_deque.kt:88-89 Line 88-89 uses !! to assert rear!!.prev and rPrev!! are non-null. While the deque invariant protect... Use requireNotNull(rear?.prev) { "Rear node must have prev when size > 1" } for better error message... 65%

Rule: bug_null_safety_kotlin



Review ID: 33b7a614-7c34-4f14-88cc-b9f45c3360f2
Rate it 👍 or 👎 to improve future reviews | Powered by diffray

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

Successfully merging this pull request may close these issues.

2 participants