problemeasyalgorithmsremove-duplicates-from-sorted-list-iremove duplicates from sorted list iremoveduplicatesfromsortedlistileetcode-83leetcode 83leetcode83

Remove duplicates from Sorted List 1

EasyUpdated: Aug 2, 2025
Practice on:
Video Solutions:

Problem

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

Examples

Example 1:

Given 1->1->2, return 1->2.
Input: head = [1,1,2]
Output: [1,2]

Example 2:

Given 1->1->2->3->3, return 1->2->3.
Input: head = [1,1,2,3,3]
Output: [1,2,3]

Similar problem - [Remove duplicates from Sorted List 2](remove-duplicates-from-sorted-list-2).

Solution

The key of this problem is using the right loop condition. And change what is necessary in each loop.

Also, we don't need to use any dummy node in linked list here, as even though the duplicates start at head, we still have another node, which we can take care of.

Method 1 - Iterative Solution

Dry Run

100%

Here is the video explanation:

<div class="youtube-embed"><iframe src="https://www.youtube.com/embed/c-5Jxa-_XAM" frameborder="0" allowfullscreen></iframe></div>

Code

Java
public ListNode deleteDuplicates(ListNode head) {
	if (head == null || head.next == null) {
		return head;
	}
	ListNode curr = head;
	while (curr.next != null) {
		if (curr.val == curr.next.val) {
			curr.next = curr.next.next;
		}
		else {
			curr = curr.next;
		}
	}
	return head;
}

Complexity

  • ⏰ Time complexity: O(n)
  • 🧺 Space complexity: O(1)

Comments