Problem statement

https://binarysearch.com/problems/Reverse-a-Linked-List/

Solution

It is equal to leetcode 0206. Reverse Linked List

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, head):
        curr = None
        nxt = head
        while nxt:
            tmp = nxt.next
            nxt.next = curr
            curr = nxt
            nxt = tmp
            
        return curr