If you meet 'runtime error', see here

  • 2

    The structure of this List here is quite different . because it doesn't have a common list head. The head of this list only stores the first element of the list. So if you always meet 'rumtime error', mostly perhaps you think it does have a true 'head' of list and have used 'NULL->next' in any way. my C solution of 3ms is here. welcome to discuss.

       struct ListNode* removeNthFromEnd(struct ListNode* head, int n) {
            struct ListNode* p1;
            struct ListNode* p2;
            struct ListNode* truehead=malloc(sizeof(struct ListNode));
            truehead->next = head;
            p1 = truehead;
            p2 = truehead;
            int cnt=0;
            while(p2->next != NULL)
                p2 = p2->next;
                if (cnt>n)
                p2 = p1->next;
                p1->next = p2->next;
            p1 = truehead->next;
            return p1;

  • 0

    Oh!!!!!!!!!!!!!You are so niubi!!!!!!!!!!!!!!!!!!!!!!!!
    It solve my question

Log in to reply

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