Your question is In-Place Linked List Reversal. 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.
Embitel Technologies uses linked structures in components such as navigation and workflow processing. Given the head of a singly linked list, reverse the list in place and return the new head.
You must modify the existing nodes rather than creating replacement nodes. The algorithm must use constant auxiliary space, excluding the input list itself.
The input is head, either None or a reference to the first ListNode. Each node has:
val: an integer valuenext: a reference to the next ListNode, or NoneReturn a reference to the former tail, which becomes the new head. The relative order of node values must be completely reversed, and every original node must appear exactly once in the result.
Assume the list is acyclic. Do not use arrays, stacks, recursion, or newly allocated list nodes.
def reverse_linked_list(head):