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]
Constraints:
- 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.
Solution
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(current.next != null && current.val == current.next.val) {
current.next = current.next.next;
}
current = current.next;
}
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.