Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions LeetCode/medium/reorder_list_143.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from typing import Optional


class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next


class Solution:
def reorderList(self, head: Optional[ListNode]) -> None:
slow, fast = head, head

while fast and fast.next:
slow = slow.next
fast = fast.next.next
half = slow.next
slow.next = None
Comment on lines +19 to +26

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Crashes on empty list input.

If head is None, slow and fast both start as None; the while fast and fast.next loop is skipped, leaving slow as None. Line 25 then does slow.next, raising AttributeError: 'NoneType' object has no attribute 'next'.

🐛 Proposed fix
     def reorderList(self, head: Optional[ListNode]) -> None:
+        if not head or not head.next:
+            return
         slow, fast = head, head
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def reorderList(self, head: Optional[ListNode]) -> None:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
half = slow.next
slow.next = None
def reorderList(self, head: Optional[ListNode]) -> None:
if not head or not head.next:
return
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
half = slow.next
slow.next = None
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@LeetCode/medium/reorder_list_143.py` around lines 19 - 26, The reorderList
method currently assumes head is non-null, which causes a crash when called with
an empty list. Update reorderList in the Solution class to handle the None case
up front before using slow.next, so the fast/slow split logic only runs when
head exists. Keep the existing midpoint logic and next-pointer splitting intact
after the early return.


prev = None
while half:
next_node = half.next
half.next = prev
prev = half
half = next_node

while head and prev:
next_head = head.next
head.next = prev
next_prev = prev.next
prev.next = next_head
head = next_head
prev = next_prev
Loading