LeetCode Remove Duplicates from Sorted List solution


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 1:

Input: head = [1,1,2]
Output: [1,2]

Example 2:

Input: head = [1,1,2,3,3]
Output: [1,2,3]


  • 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.


The problem statement here as I understand is to remove duplicate elements from LinkedList. Typically from my experience I can tell that once you understand what is a LinkedList conceptually, you will find it easier to solve easy or even medium LinkedList problems. Here my approach was I’ll iterate from beginning of LinkedList. And whenever I find a duplicate element I’ll point it’s previous element to it’s next element. I’ll keep doing this activity till I keep finding duplicate elements. This would take care of multiple duplicate elements use case. Remember, this is a sorted LinkedList. I’ll stop when I reach the end of the LinkedList.

Below is Java code for the same.

public ListNode deleteDuplicates(ListNode head) {
        ListNode current = head;
        while (current != null) {
            while( != null && current.val == {
            current =;
        return head;

And below is the efficiency from LeetCode for this solution.

Lessons Learnt

LinkedList problems are comparatively easier to solve and think through once you know the LinkedList data structure. Be careful for null checks or when you reach end of linkedlist or when to change pointers to next element of a node.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?