
AIn a Publicis Sapient engineering codebase, linked-list utilities may be used in low-level data transformations. Write a function that reverses a singly linked list and returns the new head.
Implement a function reverse_linked_list(head) where:
head is the first node of a singly linked list, or null for an empty listval and nextYou must reverse the list by changing node pointers, not by creating a second list of copied nodes.
Example 1
Input: head = [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Explanation: Each node's next pointer is reversed so traversal starts from 5 and ends at 1.
Example 2
Input: head = [7]
Output: [7]
Explanation: A single-node list is already reversed.
Example 3
Input: head = []
Output: []
Explanation: An empty list remains empty.
0 <= number of nodes <= 10^5-10^9 <= node.val <= 10^9head = [1, 2, 3, 4, 5]Output[5, 4, 3, 2, 1]WhyThe links are reversed in place, so the original tail becomes the new head.head = [7]Output[7]WhyA single node has no outgoing chain to reverse.head = []Output[]WhyAn empty list returns `None`, represented here as an empty list.0 <= number of nodes <= 10^5-10^9 <= node.val <= 10^9The list is singly linkedDo not allocate a copied replacement list for the main solutiondef reverse_linked_list(head):