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
- Sum of the nodes of a Singly Linked List
- Maximize array elements upto given number
- Check if linked list is sorted (Iterative and Recursive)
- Remove all occurrences of duplicates from a sorted Linked List
- Remove duplicates from an unsorted linked list
- Remove duplicates from a sorted linked list
- Delete all nodes from the doubly linked list which are divisible by K
- Check if two Linked Lists are permutations of each other
- Delete all Prime Nodes from a Doubly Linked List
- Print Doubly Linked list in Reverse Order
- Sort Linked List containing values from 1 to N
- Find the Second Largest Element in a Linked List
- Sort a Linked List in wave form
- Check if a Linked List is Pairwise Sorted
- Check if elements of Linked List are present in pair
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.