Reverse

Reverse a linked list given the head

public ListNode Reverse(ListNode root){
    if (root == null || root.next == null) return root;
    
    ListNode newHead = Reverse(root.next);
    root.next.next = root;
    root.next = null;
    
    return newHead;
}

TC: O(N)

SC: O(1)

Last updated

Was this helpful?