1

I am reversing a singly- linked list with the following code:

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

class Solution:
    def reverseList(self, head: ListNode) -> ListNode:
        
        if not head: 
            return []
        
        curr = head.next
        prev = head

        while curr:
            curr.next = prev 
            curr = curr.next
            prev = prev.next
        
        return curr

I am getting a TIME OUT error- but I can’t seem to see the issue. When I run the loop on paper it seems to work fine, any ideas what is causing, what I assume is the loop to not terminate? Thanks