# 83. Remove Duplicates from Sorted List

### Description

&#x20;Given a sorted linked list, delete all duplicates such that each element appear only *once*.

### Constraints

### Approach

### Links

* GeeksforGeeks
* [Leetcode](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
* [ProgramCreek](https://www.programcreek.com/2013/01/leetcode-remove-duplicates-from-sorted-list/)
* YouTube

### **Examples**

{% tabs %}
{% tab title="Example 1" %}
**Input:** 1 -> 1 -> 2

**Output:** 1 -> 2
{% endtab %}

{% tab title="Example 2" %}
**Input:** 1 -> 1 -> 2 -> 3 -> 3

**Output:** 1 -> 2 -> 3
{% endtab %}
{% endtabs %}

### **Solutions**

{% tabs %}
{% tab title="Solution 1" %}

```java
/**
 * Time complexity : O(n). Because each node in the list is checked exactly 
 *    once to determine if it is a duplicate or not, the total run time 
 *    is O(n), where n is the number of nodes in the list.
 * Space complexity : O(1). No additional space is used.
 */

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null) return null;
        ListNode curr = head;
        while(curr.next != null) {
            if(curr.val == curr.next.val) {
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return head;
    }
}
```

{% endtab %}
{% endtabs %}

### **Follow up**

* Remove duplicates from an unsorted linked list - [GFG](https://www.geeksforgeeks.org/remove-duplicates-from-an-unsorted-linked-list/)
* Merge two sorted linked list without duplicates - [GFG](https://www.geeksforgeeks.org/merge-two-sorted-linked-list-without-duplicates/)
