class Solution(object):
    def reverseList(self, head):
        """
        :type head: ListNode
        :rtype: ListNode
        """
        p = head
        q = None
        while p:
            c = p.next
            p.next = q
            q = p
            p = c
        return q
Copy the code