Write a removeDuplicates() function which takes a list sorted in non-decreasing order and deletes any duplicate nodes from the list. The list should only be traversed once.
For example if the linked list is 11->11->11->21->43->43->60 then removeDuplicates() should convert the list to 11->21->43->60.
Traverse the list recursively from the head (or start) to end and after completion of recursion calls, compare the next node(returned node) and current node(head). If data of both nodes are equal then return the next (head-> next) node else return the current node(head).
Functions other than removeDuplicates() are just to create a linked linked list and test removeDuplicates().
Linked list before duplicate removal 11 11 11 13 13 20 Linked list after duplicate removal 11 13 20
- Remove duplicates from a sorted linked list
- Remove duplicates from a sorted doubly linked list
- Remove all occurrences of duplicates from a sorted Linked List
- Remove duplicates from an unsorted linked list
- Remove duplicates from an unsorted doubly linked list
- Merge two sorted linked list without duplicates
- Replace nodes with duplicates in linked list
- Insert value in sorted way in a sorted doubly linked list
- Delete a linked list using recursion
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Reverse a Doubly linked list using recursion
- Practice questions for Linked List and Recursion
- Given a linked list which is sorted, how will you insert in sorted way
- Print alternate nodes of a linked list using recursion
- Remove last node of the linked list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.