给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
示例 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) { struct ListNode* h = head; struct ListNode* cur = h; if(cur == NULL || cur->next == NULL) return cur; while(cur) { struct ListNode* tmp = cur->next; if(tmp && cur->val == tmp->val) { cur->next = tmp->next; free(tmp); continue;/*继续比较下一个元素*/ } cur = cur->next; } return h; }