Getting Runtime errr?althougn running fine on my compiler


  • 0
    D
    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* reverseKGroup(ListNode* head, int k) {
            ListNode* cur;
            ListNode* temp;
            ListNode* kth;
            ListNode* prev;
            cur=head;
            temp=head;
            int count=0;
            while(count!=k)
            {
                count++;
                cur=cur->next;
            }
            kth=cur;
            prev=temp->next;
            while(1)
            {
                temp->next=cur;
                cur=temp;
                if(prev==kth )
                    break;
                    temp=prev->next;
    
                prev->next=cur;
                cur=prev;
    
                if(temp==kth)
                    break;
                prev=temp->next;
            }
            head=cur;
           // display();
            return head;
     
        }
    };

  • 0
    B

    your first while loop should check for cur==NULL.. something like this:

           while(count!=k && cur!=NULL)
            {
                count++;
                cur=cur->next;
            }
    

Log in to reply
 

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