Dont' know why my code has runtime error


  • 0
    D
    /**
    
    • Definition for singly-linked list.

    • struct ListNode {

    • int val;
      
    • ListNode *next;
      
    • ListNode(int x) : val(x), next(NULL) {}
      
    • };
      */
      class Solution {
      public:
      ListNode *removeNthFromEnd(ListNode *head, int n) {
      ListNode *nodePtr;
      ListNode *r;
      ListNode *previousPtr;

       nodePtr = head; 
       r = head; 
       previousPtr = head; 
       
       int count; 
       
       if(head){
           while (count < n){
               r = r->next; 
               count ++; 
           }
           while (r != NULL){
               r = r->next;
               previousPtr = nodePtr; 
               nodePtr = previousPtr->next; 
           }
           previousPtr->next = nodePtr->next; 
           delete nodePtr; 
       }
      

      }
      };


  • 0
    Y
    > int count; 
    > 
    >     if(head){
    >         while (count < n){
    

    1-forget to initialize count

    2-no return value

    maybe still other mistakes


Log in to reply
 

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