Given a linked list. Find the first element from the left which appears more than once. If all the elements are unique then print -1.
Input : 1 2 3 4 3 2 1
Output : 1
In this linked list the element 1 occurs two times
and it is the first element to satisfy the condition.
Hence the answer is 1.
Input : 1 2, 3, 4, 5
Output : -1
All the elements are unique. Hence the answer is -1.
- Count the frequency of all the elements of the linked list using a map.
- Now, traverse the linked list again to find the first element from the left whose frequency is greater than 1.
- If no such element exists then print -1.
Below is the implementation of the above approach:
Time Complexity : O(N)
- Find the Second Largest Element in a Linked List
- Find a peak element in Linked List
- Create new linked list from two given linked list with greater element at each node
- Create a linked list from two linked lists by choosing max element at each position
- Second Smallest Element in a Linked List
- Move first element to end of a given Linked List
- Majority element in a linked list
- Rearrange a linked list in to alternate first and last element
- Move last element to front of a given Linked List | Set 2
- Move all occurrences of an element to end in a linked list
- Move last element to front of a given Linked List
- Search an element in a Linked List (Iterative and Recursive)
- Maximum and Minimum element of a linked list which is divisible by a given number k
- Find the sum of last n nodes of the given Linked List
- Find sum of even and odd nodes in a 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.