leetcode 83. Remove Duplicates from Sorted List
Description
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,
Given 1->1->2
, return 1->2
.
Given 1->1->2->3->3
, return 1->2->3
.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* deleteDuplicates(struct ListNode* head) {
struct ListNode *pre, *cur, *q;
if(head == NULL) return NULL;
pre = head;
cur = pre->next;
while(cur){
if(cur->val == pre->val){
q=cur;
cur = cur->next;
pre->next = cur;
free(q);
}else{
pre = cur;
cur = cur->next;
}
}
pre->next = NULL;
return head;
}