Memory limit exceeded because of one variable?


  • -1
    D

    In my solution for a problem I originally used 3 reference variables, which was apparently enough to cause a memory limit error. I figured out a way to get rid of one of them, and my solution was accepted. Isn't it a bit extreme that one variable causes such an error?


  • 0

    Keeping the problem and your code secret doesn't help at all. How is anybody supposed to look into this?


  • 0
    D

    I was very tired, I didn't think of submitting code. This was the original code.

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public ListNode rotateRight(ListNode head, int k) {
            if (head == null) return null;
            if (k == 0) return head;
            
            int i = 0;
            
            ListNode h = head;
            while ((i < k) && (h != null))
            {
                h = h.next;
                i++;
            }
                
            // k > list size
            if (h == null)
            {
                k = k % i;
                i = 0;
                h = head;
                while (i++ < k)
                    h = h.next;
                    
                if (k == 0) return head;
            }
            
            ListNode t = head;
            while (h.next != null)
            {
                h = h.next;
                t = t.next;
            }
            
            ListNode r = t.next;
            t.next = null;
            h.next = head;
            return r;
        }
    }
    

    After I got the error I changed the last 4 lines and it worked fine.

            // Same code as above until last 4 lines
    
            // ListNode r = t.next;
            // t.next = null;
            // h.next = head;
            // return r;
            
            h.next = head;
            head = t.next;
            t.next = null;
            return head;
        }
    }
    
    

    I tried the original code again this morning, somehow it works now. I don't know why it complained before. I don't think I was not hallucinating last night.


  • 0

    @diver said in Memory limit exceeded because of one variable?:

    I don't think I was not hallucinating last night.

    So you think you were hallucinating? :-)

    Still not linking to the problem, but I found it. Anyway, I submitted that and it got accepted as well. Are you sure that's the code that didn't get accepted? You can click on "My Submissions", click on the error status, and then copy the code that you submitted there.


  • 0
    D

    @ManuelP Sorry, that's a typo :) Maybe I am hallucinating after all. It is definitely working now. I looked at all my submissions; there is nothing that says memory limit exceeded :/


Log in to reply
 

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