定义两个链表指向当前和下一个进行对比,保存当前的链表为输出。
struct ListNode* deleteDuplicates(struct ListNode* head){
if(head == NULL || head->next == NULL)return head;
struct ListNode *slow = head;
struct ListNode *fast = head->next;
while(fast != NULL){
if(slow->val != fast->val){
slow->next = fast;
slow = fast;
}
fast = fast->next;
}
slow->next = NULL;
return head;
}