#83. remove-duplicates-from-sorted-list

**<font color=red>难度:Easy</font>**

## 刷题内容

> 原题连接

*https://leetcode.com/problems/remove-duplicates-from-sorted-list/
* 
> 内容描述

```
Given a sorted linked list, delete all duplicates such that each element appear only once.

Example 1:

Input: 1->1->2
Output: 1->2
Example 2:

Input: 1->1->2->3->3
Output: 1->2->3
```

## 解题方案

> 思路
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******


题目中给出的链表是已经排序好的，那么我们只需要遍历链表，当后一个节点的数等于当前节点的，就删去


```cpp
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* deleteDuplicates(ListNode* head) {
        ListNode* l = head;
        ListNode* l1 = head;
        if(l != nullptr)
        {
                while(l ->next != nullptr)
            {
                if(l ->val == (l ->next ->val))
                    l ->next = l ->next ->next;
                else
                    l = l ->next;
            }
        }
           return l1;
    }
};
```