AttributeError: 'NoneType' object has no attribute 'next'
python-forum.io › thread-31503class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next class Solution: def removeNthFromEnd(self, head: ListNode, n: int) -> ListNode: total = 0 temp = head while temp is not None: temp = temp.next total += 1 k = total - n prev = None curr = head while k > 0: prev.next = curr curr = curr.next k -= 1 if prev is None: return head.next else: prev.next = curr.next return head list1 = ListNode(2); list1.next = ListNode(4) list1.next.next = ListNode(3) list1.next ...
Getting AttributeError: 'list' object has no attribute 'next'
stackoverflow.com › questions › 54084552Jan 08, 2019 · Show activity on this post. I am getting AttributeError: 'list' object has no attribute 'next' when I am trying to reverse a linked list. class ListNode: def __init__ (self, x): self.val = x self.next = None class Solution: def reverseList (self, head): prev = None while head: curr = head head = head.next curr.next = prev prev = curr return prev s = Solution () s.reverseList ( [0,1,2,3,4,5])