```python class ListNode: """Definition for singly-linked list node.""" def __init__(self, val=0, next=None): self.val = val self.next = next def reverse_linked_list(head: ListNode) -> ListNode: """ Reverses a singly linked list and returns the new head. Args: head: The head node of the singly linked list. Returns: The new head node of the reversed linked list. """ prev = None current = head while current: next_node = current.next # Store the next node current.next = prev # Reverse the link prev = current # Move prev one step forward current = next_node # Move current one step forward return prev # prev is the new head of the reversed list def list_to_array(head: ListNode) -> list: """Helper function to convert linked list to array for testing.""" result = [] while head: result.append(head.val) head = head.next return result def array_to_list(arr: list) -> ListNode: """Helper function to convert array to linked list for testing.""" if not arr: return None head = ListNode(arr[0]) current = head for val in arr[1:]: current.next = ListNode(val) current = current.next return head if __name__ == "__main__": # Test case 1: Normal list arr = [1, 2, 3, 4, 5] head = array_to_list(arr) reversed_head = reverse_linked_list(head) print(list_to_array(reversed_head)) # Expected: [5, 4, 3, 2, 1] # Test case 2: Empty list head = array_to_list([]) reversed_head = reverse_linked_list(head) print(list_to_array(reversed_head)) # Expected: [] # Test case 3: Single element head = array_to_list([1]) reversed_head = reverse_linked_list(head) print(list_to_array(reversed_head)) # Expected: [1] # Test case 4: Two elements head = array_to_list([1, 2]) reversed_head = reverse_linked_list(head) print(list_to_array(reversed_head)) # Expected: [2, 1] ```