Why Submission Details is Runtime Error?


  • 0
    L
    int k = 0;
            ListNode dummyHead(0); 
            dummyHead.next = head;
            ListNode *p,*q;
            p = &dummyHead;
            q = &dummyHead;
            if (head == NULL)
                return head;
            while (p != NULL)
            {
                k++;
                p = p->next;
            }
            k = k-n;
            while (q != NULL && k>0)
            {
                k--;
                q = q->next;
            }
            q->next = q->next->next;
        
            return dummyHead.next;

  • 0
    M
    1. Why did you put the check point to first position?
      if (head == NULL)
      return head;
    2. I don't know your Language, but here's list, your Pointer(address of every node maybe not contiguous) has problem. So k = k-n; is not efficient.

Log in to reply
 

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