# 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/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://code-snippets.hbamithkumara.com/leetcode/problems/1-100/remove-duplicates-from-sorted-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
