力扣6

 

给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
if (head == NULL){
return head;
}
struct ListNode *ptr1 = head;
struct ListNode *ptr2;
int cur = 0;

while (ptr1->next != NULL){
cur = ptr1->val;
ptr2 = ptr1->next;
while (ptr2->val == cur){
if (ptr2->next == NULL){
ptr1->next = NULL;
return head;
}
else
ptr2 = ptr2->next;
}
ptr1->next = ptr2;
ptr1 = ptr1->next;
}
return head;

}

posted on 2019-04-15 09:24  xiegangqingnian  阅读(128)  评论(0编辑  收藏  举报

导航