Java solution, got MLE...


  • 0
    M
    public ListNode reverseListNode(ListNode head, int len){
        if(len > 1){
            ListNode p = head, prev = new ListNode(-1);
            while(len > 0){
                int count = 1;
                boolean isHead = true;
                prev.next = p = head;
                while(p.next != null && count < len){
                    ListNode q = p.next;
                    p.next = q.next;
                    q.next = p;
                    prev.next = q;
                    prev = prev.next;
                    if(isHead){
                        isHead = false;
                        head = q;
                    }
                    count ++;
                }
                len--;
            }
        }
        return head;
    }
    
    public ListNode reverseBetween(ListNode head, int m, int n) {
        if(head == null || head.next == null) return head;
        int i = 1;
        ListNode p = head;
        while(i < m){
            p = p.next;
            i++;
        }
        ListNode ln = reverseListNode(p, n);
        if(m == 1)
            head = ln;
        return head;
    }
    

    I don’t get that why my code got Memory Limited Exceed... the issue may be in the function of reverseListNode.


  • 0
    This post is deleted!

  • 0

    I had the same issue , the reason I figured out was when all the operations were over , the linked list had cycles in it .

    Try to print your entire linked list before return statement , then
    you can figure out what changes are to be made.


Log in to reply
 

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