---
title: 'LeetCode 83. Remove Duplicates from Sorted List'
disqus: hackmd
---
# LeetCode 83. Remove Duplicates from Sorted List
## Description
Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.
## Example
Input: head = [1,1,2]
Output: [1,2]
Input: head = [1,1,2,3,3]
Output: [1,2,3]
## Constraints
The number of nodes in the list is in the range [0, 300].
-100 <= Node.val <= 100
The list is guaranteed to be sorted in ascending order.
## Answer
此題如果現在的值跟下一個相同就將下一個值用下下一個代替,直到值不同了才將node往下移動。因檢查到opr->next->next所以while條件注意為檢查到opr->next != NULL,如此才不出現NULL pointer。
```Cin=
//2022_03_30
struct ListNode* deleteDuplicates(struct ListNode* head){
struct ListNode *del = NULL, *opr = head;
while(opr != NULL && opr->next != NULL){
if(opr->val == opr->next->val){
del = opr->next;
opr->next = opr->next->next;
free(del);
}
else{opr = opr->next;}
}
return head;
}
```
## Link
https://leetcode.com/problems/remove-duplicates-from-sorted-list/
###### tags: `Leetcode`