
In Snowflake-style coding interviews, a common data structure task is to reverse a singly linked list in place. Write a function that takes the head of a singly linked list and returns the head of the reversed list.
head, the first node of a singly linked list, or null for an empty list.val and next.Example 1
head = [1, 2, 3, 4, 5][5, 4, 3, 2, 1]next pointer is reversed, so the tail becomes the new head.Example 2
head = [7][7]Example 3
head = [][]0 <= number of nodes <= 5000-10^4 <= Node.val <= 10^4O(n) timeO(1) extra space for the main solutionhead = [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 links to reverse, so the list stays the same.head = []Output[]WhyAn empty list has no nodes, so the result is also empty.0 <= number of nodes <= 5000-10^4 <= Node.val <= 10^4Each node contains `val` and `next`Do not allocate a copied list for the main solutiondef reverse_linked_list(head):