删除已排序链表中的重复项-code not working.What 是错误吗?

Remove duplicates in sorted linked list-code not working.What is the error?

删除排序链表中的重复项-代码不是working.What是错误吗?

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        if(head==NULL || head->next==NULL)return head;
        ListNode* curr=head;
        while(curr!=NULL)
        {
            while(curr->val==curr->next->val){
                curr->next=curr->next->next;
            }
            curr=curr->next;    
        }
        return head;
    }
};

http://notes.io/qs1p

您对删除重复项的逻辑有疑问。将您的 while 循环更改为:

    while (curr != NULL) {
        ListNode* index = curr;
        while (curr->val == index->next->val) {  // check for multiple possible duplicates
            index = index->next;                 // and advance index to last duplicate
        }
        curr->next = index->next;                // splice out the duplicate (if it exists)
        curr = curr->next;                       // advance pointer
    }