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
30 changes: 30 additions & 0 deletions LeetCode/medium/remove_nth_from_end_19.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from typing import Optional


class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
Comment on lines +5 to +7

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.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Rename next to avoid builtin shadowing.
This keeps lint noise down and avoids reusing a builtin name.

🧰 Tools
🪛 Ruff (0.15.18)

[error] 5-5: Function argument next is shadowing a Python builtin

(A002)

🤖 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/remove_nth_from_end_19.py` around lines 5 - 7, Rename the
ListNode initializer parameter in the ListNode class from next to a non-builtin
name and update the assignment accordingly; make the change in the __init__
method so the constructor signature and any internal references no longer shadow
the builtin name.



class Solution:
def length(self, head: ListNode) -> int:
count = 0
current = head
while current:
count += 1
current = current.next
return count

def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
n_len = self.length(head=head)
position = n_len - n
dummy = ListNode(0)
dummy.next = head
prev = dummy

for _ in range(position):
prev = prev.next
prev.next = prev.next.next

return dummy.next
Loading