I'm getting a loop in linked list error while trying to reverse a linked list-
class Solution:
def reverseList(self, head: ListNode) -> ListNode:
first = head
second = head.next
third = head.next.next
while third != None and third.next != None:
third = second.next
second.next = first
first = second
second = third
print(first)
print (second)
return second
Output : [5]
And the error is-
Error - Found cycle in the ListNode ListNode{val: 5, next: None}