Getting runtime error

  • 2

    Hi. I'm getting a runtime error for this with Last executed input: {5}, 1, 1

    The code is not great, but i've tested it on my machine and it appears to work. Moreover, because of the very first if condition, I have no idea how that test case could fail :S

    public ListNode reverseBetween(ListNode head, int m, int n) {
    	if(head == null || m == n) return head;
    	ListNode head_ = head; // save old head
    	for(int i = 1; i < m-1; i++) head =;
    	ListNode start = head;
    	ListNode revEnd = m == 1 ? head :;
    	ListNode prev = revEnd;
    	head = prev;
    	for(int i = m; i <= n; i++){
    		ListNode next =; = prev;
    		prev = head;
    		head = next;
    	// Thread appropriately = head;
    	if(m == 1) head_ = prev; // if we need to replace the head
    	else = prev;
    	return head_;

    Anyone see any problems? I'm surprised since it shouldn't get any farther than the first if statement. Thanks.

  • 0

    That's curious, indeed. I tried your code and it crashed with a runtime error; then I tried mine and it went through without problem. I wonder if your problem is not with another test case. In particular, are you sure you don't get a NullPointerException for the instruction = prev when i==n==length of the list?

  • 0

    Yeah, it's probably crashing on some other test case. But I still can't figure it out, tested it on all 1 <= m <= n <= length of list for a list of size 1, 2, 3, 4, 5 and I don't get any errors.

  • 0

    Sorry, I have fixed this issue. Your code is AC now :)

  • 0

    It was an OJ problem? What was it exactly, I'm curious now :)

  • 0

    I'm also very curious!

  • 0

    Hi! I am getting similar problem. It shows last executed input as [3,5],1,2 and showing run time error but it works fine. I have tested all cornet cases as far as I understand.Can anybody help me out here :

    class Solution {
    	//reverse from head to given count and dont consider remaining part if attached
        ListNode* reverseCount(ListNode *head, int count){		
    		ListNode* cur = head;
    		if(head->next==NULL)return head;
    		int mycount = 1;
    		ListNode *curnext = cur->next,*temp;
    		cur->next = NULL;
    			temp = curnext->next;
    			curnext->next = cur;
    			cur = curnext;
    			curnext = temp;
    		}while(curnext!=NULL && mycount<count);
    		head = cur;		
    		return head;
        ListNode* reverseBetween(ListNode* head, int m, int n) {
        	if(head==NULL || head->next == NULL || m==n)return head;    	
        	ListNode *cur,*prevhead,*nextappend,*toReverse,*temp;
        	int pos = 1,count = n-m+1;
        	bool isFirst = false;
        		isFirst = true;
        		toReverse = cur;
        		temp = cur;
        		cur = head;
        			cur = cur->next;
        		prevhead =  cur;
        		toReverse = prevhead->next;    		
        		temp = cur->next;
        	//get the remaining list after nth pos
        	for(int j=0;j<count;j++){
        		temp = temp->next;
        	//reverse list from m to n
        	cur = reverseCount(toReverse,count);
        	//adjust starting part's pointers
            if(!isFirst)prevhead->next = cur;
            else head = cur;
            //get the end point of m-n list
            for(int k=0;k<count-1;k++){
            	cur = cur->next;
            //append the last list to end point of m-n list
            cur->next = temp;
            return head;
        void print(ListNode* head){
        	ListNode* cur = head;
        		cout<<cur->val<<" ";
        		cur = cur->next;

Log in to reply

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