Memory Limit Exceeded


  • 0
    K

    Any idea why my code is throwing MLE?

    public class Solution {
    
        public ListNode reverse(ListNode head, ListNode prev, int count) {
            int i = 0;
            
            while(i < count) {
                ListNode temp = head.next;
                head.next = prev;
                prev = head;
                head = temp;
                i++;
            }
            return prev;
        }
        
        public ListNode reverseBetween(ListNode head, int m, int n) {
            if(head == null) return null;
            
            ListNode curr = head, prev = null;
            int i=1;
            while(i < m) {
                prev = curr;
                curr = curr.next;
                i++;
            }
            
            if(prev != null) {
                prev.next = reverse(curr, prev, n-m+1);
                return head;
            }
            else return reverse(curr, prev, n-m+1);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.