# First non-repeating in a linked list

Given a linked list, find its first non-repeating integer element.

Examples:

Input : 10->20->30->10->20->40->30->NULL Output :First Non-repeating element is 40. Input :1->1->2->2->3->4->3->4->5->NULL Output :First Non-repeating element is 5. Input :1->1->2->2->3->4->3->4->NULL Output :No NOn-repeating element is found.

1) Create a hash table and marked all element as zero.

2) Traverse the linked list and count the frequency of all the element in hashtable.

3) Traverse the linked list again and see the element who’s frequency is 1 in hashtable.

`// C++ program to find first non-repeating ` `// element in a linked list ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `/* Link list node */` `struct` `Node ` `{ ` ` ` `int` `data; ` ` ` `struct` `Node* next; ` `}; ` ` ` `/* Function to find the first non-repeating ` ` ` `element in the linked list */` `int` `firstNonRepeating(` `struct` `Node *head) ` `{ ` ` ` `// Create an empty map and insert all linked ` ` ` `// list elements into hash table ` ` ` `unordered_map<` `int` `, ` `int` `> mp; ` ` ` `for` `(Node *temp=head; temp!=NULL; temp=temp->next) ` ` ` `mp[temp->data]++; ` ` ` ` ` `// Traverse the linked list again and return ` ` ` `// the first node whose count is 1 ` ` ` `for` `(Node *temp=head; temp!=NULL; temp=temp->next) ` ` ` `if` `(mp[temp->data] == 1) ` ` ` `return` `temp->data; ` ` ` ` ` `return` `-1; ` `} ` ` ` `/* Function to push a node */` `void` `push(` `struct` `Node** head_ref, ` `int` `new_data) ` `{ ` ` ` `struct` `Node* new_node = ` ` ` `(` `struct` `Node*) ` `malloc` `(` `sizeof` `(` `struct` `Node)); ` ` ` `new_node->data = new_data; ` ` ` `new_node->next = (*head_ref); ` ` ` `(*head_ref) = new_node; ` `} ` ` ` `/* Driver program to test above function*/` `int` `main() ` `{ ` ` ` `// Let us create below linked list. ` ` ` `// 85->15->18->20->85->35->4->20->NULL ` ` ` `struct` `Node* head = NULL; ` ` ` `push(&head, 20); ` ` ` `push(&head, 4); ` ` ` `push(&head, 35); ` ` ` `push(&head, 85); ` ` ` `push(&head, 20); ` ` ` `push(&head, 18); ` ` ` `push(&head, 15); ` ` ` `push(&head, 85); ` ` ` `cout << firstNonRepeating(head); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Output:

15

**Further Optimizations:**

The above solution requires two traversals of linked list. In case we have many repeating elements, we can save one traversal by storing positions also in hash table. Please refer last method of Given a string, find its first non-repeating character for details.

## Recommended Posts:

- Create new linked list from two given linked list with greater element at each node
- XOR Linked List – A Memory Efficient Doubly Linked List | Set 2
- XOR Linked List - A Memory Efficient Doubly Linked List | Set 1
- Difference between Singly linked list and Doubly linked list
- Convert singly linked list into circular linked list
- Merge a linked list into another linked list at alternate positions
- Convert Singly Linked List to XOR Linked List
- Check if a linked list is Circular Linked List
- Construct a Maximum Sum Linked List out of two Sorted Linked Lists having some Common nodes
- Create a linked list from two linked lists by choosing max element at each position
- Length of longest palindrome list in a linked list using O(1) extra space
- Partitioning a linked list around a given value and If we don't care about making the elements of the list "stable"
- Rotate the sub-list of a linked list from position M to N to the right by K places
- Generic Linked List in C
- Linked List Implementation in C#

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.