Getting runtime error


  • 2
    R

    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 = head.next;
    	
    	ListNode start = head;
    	ListNode revEnd = m == 1 ? head : head.next;
    	ListNode prev = revEnd;
    	head = prev;
    
    	for(int i = m; i <= n; i++){
    		ListNode next = head.next;
    		head.next = prev;
    		prev = head;
    		head = next;
    	}
    
    	// Thread appropriately
    	revEnd.next = head;
    	
    	if(m == 1) head_ = prev; // if we need to replace the head
    	else 	   start.next = prev;
    
    	return head_;
    }
    

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


  • 0
    L

    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 head.next = prev when i==n==length of the list?


  • 0
    R

    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
    R

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


  • 0
    L

    I'm also very curious!


  • 0
    D

    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 {
    public:
    
    	//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;
    
    		do{
    			temp = curnext->next;
    
    			curnext->next = cur;
    
    			cur = curnext;
    			curnext = temp;
    			mycount++;
    		}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;
    
        	if(m==1){
        		isFirst = true;
        		toReverse = cur;
        		temp = cur;
        	}else{
        		cur = head;
        		while(pos<m-1){
        			cur = cur->next;
        			pos++;
        		}    		
        		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;
        	while(cur!=NULL){
        		cout<<cur->val<<" ";
        		cur = cur->next;
        	}
        	cout<<endl;
        }
    };

Log in to reply
 

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