Reverse a singly linked list

Given a singly linked list, reverse the order of the elements in the list.

Problem

Reverse a singly linked list.
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Solution

This solution is in Python. Our A.I. can create solutions in multiple languages.
by kayakane
# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

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

A.I. Evaluation of the Solution

This is a good solution that correctly reverses a singly linked list. The approach is clear and easy to follow.

Evaluated at: 2022-11-22 14:15:41