Your question is Linked List Inversion. Start with the requirements on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
A Graviton Research Capital processing stream stores ordered events in a singly linked list. Reverse the list in place so the events appear in the opposite order, then return the new head so the resulting sequence can be printed.
Each node has an integer val field and a next field pointing to the next node or None. Implement reverse_linked_list(head), where head is the first ListNode or None. Return the head of the reversed list. The caller will print the returned list from left to right. Do not create new list nodes, and do not convert the list into an array.
For test cases, linked lists are represented as arrays for serialization. For example, [1, 2, 3] represents 1 -> 2 -> 3.
def reverse_linked_list(head):