给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 1:
输入: 1->1->2 输出: 1->2
示例 2:
输入: 1->1->2->3->3 输出: 1->2->3
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* deleteDuplicates(struct ListNode* head) { if (head == NULL) { return NULL; } struct ListNode *p=head; struct ListNode *s=p->next; while(p->next != NULL) { if (p->val == s->val) { if (s->next != NULL) { p->next = s->next; s=s->next; } else { p->next =NULL; break; } } else { s=s->next; p=p->next; } } return head; }