Remove Duplicates from Sorted List
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 *L, *tmp;
if(head == NULL ) return NULL;
L = head;
while(L->next != NULL){
if (L->next->val == L->val)
{
tmp = L->next;
L->next = tmp->next;
free(tmp);
}
else L=L->next;
if(L == NULL) break;
}
return head;
}