Remove duplicates from a sorted linked list

Introduction

Before reading this article, It is recommended to have an understanding of the linked list and its implementation and some basic operations on the linked list.

Problem Description

Create a function that takes a non-descending list and removes any duplicate nodes. Only one traversal of the list is necessary. Eg. Consider the given linked list: 1->1->1->2->2->3, the function is expected to convert the linked list into 1->2->3.

Important: We are supposed to delete only the duplicates i.e. only one copy of nodes should be there.

Intuition: The biggest hint given is that the list is sorted or non-decreasing in nature. From this, we can deduce that if duplicates are present, then it has to be the next one in the chain.

The ideal way to approach this problem is to simply loop through the first value in a duplicate set until you locate a non-duplicate value or reach the end of the list. Adjust the next value of the node to point to the location after the "inner" loop when you obtain a non-duplicate or reach the end.

First, we'll see if the List is empty. Curr should be set to head, and next to curr.next. We'll loop until the next is null; if curr is the only node in the list, the loop will be skipped. Use a while loop to fetch the node after next once you've found a duplicate.

If a non-duplicate is detected, exit the loop; else, the next will reach the end and be null. Set next to curr.next and curr.next to next, then curr = curr.next;. Finally, because no modifications were made to that reference, nodes after it was changed, return head.

/**

  • struct ListNode {
  • int val;
  • ListNode *next;
  • ListNode() : val(0), next(nullptr) {}
  • ListNode(int x) : val(x), next(nullptr) {}
  • ListNode(int x, ListNode *next) : val(x), next(next) {}
  • }; */

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

## Frequently Asked Questions

What are the applications of linked lists? Applications of linked lists are: Here you don’t need to know the size at declaration time. It will let you insert elements at the beginning and end of the list.

What is the difference between singly & doubly linked lists? Doubly Linked List has two links to previous and forward nodes: one to point to the previous node and the other to point to the next node.

What is the advantage of linked lists? The advantages of linked lists: The advantage of linked lists is you do not specify a fixed size at declaration time. The elements you add to the chain, the bigger the chain gets.

What are the four types of a linked list? Singly-linked lists. Doubly linked lists. Circular linked lists. Circular doubly-linked lists.

What are the parts of a linked list? Linked lists consist of nodes. Each node has data and a reference to the next pointer.

### Conclusion

In this article, we have extensively discussed the Label Control in C# & how to remove duplicates from sorted list. We hope that this blog has helped you enhance your knowledge regarding Label Control.