Let’s solve a linked list problem. See the problem in Leetcode. Click Here.

The problem description is pretty short and simple. It says that we are will be given a linked list. We have to delete all duplicates such that each element appears only once.

Suppose the given linked list is: **1 -> 2 -> 2 -> 3 -> 4 -> 4 -> 5**. In the given list, there is two duplicates node. We have to delete those node. And after the deletion, the list will be: **1 -> 2 -> 3 -> 4 -> 5**.

## Coding Part

In the problem, we have access to the head node. So, we will run a loop. In the loop, we will check if the current node value is similar to the next node value. If it is similar, then we will skip the next node. Otherwise, we will just continue the loop until it reaches the end.

After finishing the loop, we will just return the head node. Let’s see the code below. The code can explain better than me.

class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: current = head while current and current.next: if current.val == current.next.val: current.next = current.next.next else: current = current.next return head

That was my solution. There are many different types of solutions to this problem. You can also try those solutions.

Happy Coding π

Keep practicing π