Why MLE for my program?


  • 0
    M
    public class Solution {
        public ListNode reverseKGroup(ListNode head, int k) {
            if (head==null)
                return null;
            ListNode[] list=reverseNode(head, k);
            ListNode newHead=list[0];
            while (list[2]!=null) {    //reverse every k nodes
                ListNode r=list[1];
                ListNode h=list[2];
                list=reverseNode(h, k);
                r.next=list[0];
            }
            return newHead;
        }
        
        private ListNode[] reverseNode(ListNode head, int k) {    //return value 0: new head, 1 new rear, 2 next head
            ListNode[] res=new ListNode[3];
            int count=1;
            ListNode rear=head;
            while (count<k && rear.next!=null) {      //count k nodes to do the reverse
                count++;
                rear=rear.next;
            }
            if (count<k) {    //return if less than k nodes
                res[0]=head;
                res[1]=rear;
                res[2]=null;
                return res;
            }
            res[2]=rear.next;
            ListNode p=head, q=head.next;
            while (p!=rear) {      //reverse the nodes one by one, that's where the problem emerges
                ListNode r=q.next;
                q.next=p;
                p=q;
                q=r;
                count--;
            }
            res[0]=rear;
            res[1]=head;
            return res;
        }
    }
    

    MLE test case: [1,2,3] 3
    actually if list size % k = 0, the program always gets a MLE
    when I run locally, everything goes well...


  • 0

    @Managine Please add some explanation or comments to make it easier to understand your code to really easier to help you!

    Try to ease the burden of the helpers, which will make your problem be solved more quickly.


Log in to reply
 

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