Find first non-repeating character in a given string using Linked List

Given a string str of length L, the task is to find the first occurrence of a non-repeating character in the string.

Input: str = “geeksforgeeks”
Output: f

Input: str = “programmer”
Output: p


Note: Please refer this article for HashMap approach and this article as space-optimized approach.

Linked List Approach:  The idea is to use Linked List to keep track of the unique elements in the string. Below is the illustration of the approach:

  • Iterate over the string for each character in the string and add the character in the Linked List on basis of the below conditions:
    • If the character is already present in the Linked List, then remove the existing character node from the linked list.
    • Otherwise, Add the character into the linked list.
  • Finally, the character at the first node of the linked list is the first non-repeating character of the string.

Below is the implementation of the above approach:






// Java implementation to find the
// first non-repeating element
// of the string using Linked List
import java.util.LinkedList;
public class FirstNonRepeatingElement {
    // Function to find the first
    // non-repeating element of the
    // given string using Linked List
    static void firstNonRepElement(String str)
        LinkedList<Character> list
            = new LinkedList<Character>();
        for (int i = 1; i < str.length(); i++) {
            if (list.contains(str.charAt(i)))
    // Driver Code
    public static void main(String[] args)
        String str = "geeksforgeeks";
        // Function Call




Performance Analysis:

  • Time Complexity: O(N * 26)
  • Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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