Right Now I'm trying my first medium leetcode problem for the first time (Remove Nth Node From End of List) and I'm pretty confident that the code I have should work with it. But for some reason when I it runs, my while-loop for A.Next
gives the error:
NoneType' object has no attribute 'next'
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def removeNthFromEnd(self, head: Optional[ListNode], n: int) -> Optional[ListNode]:
a = head
b = head
c = head
i = 0
while i < n - 1:
c = c.next
i += 1
while c.next != None:
b = b.next
c = c.next
while a.next != b:
a = a.next
a.next = b.next
b = None
return head
It doesn't make sense as to why its not running because I defined my a
variable to equal the head which should then also have access to a.next
because again, its connected to the head. The while c.next != None:
loop works fine without any issues so I don't understand whats causing the issue for my a
variable.