I WAS WRITING CODE TO REVERSELINKED(recursively) ON LEETCODE QUES 206. PLS CHECK MY CODE AND EXPLAIN ME MY MISTAKE

link to my code-https://onlinegdb.com/zFEIHqnRN_

it was showing not giving any output pls check.

you are getting tle
see this:

public ListNode reverseList(ListNode head) {
    if (head == null || head.next == null) return head;
    ListNode p = reverseList(head.next);
    head.next.next = head;
    head.next = null;
    return p;
}

Complexity analysis

  • Time complexity : O(n)O(n). Assume that nn is the list’s length, the time complexity is O(n)O(n).
  • Space complexity : O(n)O(n). The extra space comes from implicit stack space due to recursion. The recursion could go up to nn levels deep.