From 206. Reverse Linked List

Description

Given the head of a singly linked list, reverse the list, and return the reversed list.

Solution

class Solution:
    def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
      cur, prev = head, None
      while cur:
        cur.next, prev, cur, = prev, cur, cur.next
      return prev