Convert a String to a Singly Linked List Last Updated : 22 Dec, 2022 Comments Improve Suggest changes Like Article Like Report Given string str, the task is to convert it into a singly Linked List. Examples: Input: str = "ABCDABC" Output: A -> B -> C -> D -> A -> B -> C Input: str = "GEEKS" Output: G -> E -> E -> K -> S Approach: Create a Linked ListFetch each character of the string and insert it into a new node in the Linked ListPrint the Linked List Below is the implementation of the above approach: C++ // C++ program to Convert a String // to a Singly Linked List #include <iostream> using namespace std; // Structure for a Singly Linked List struct node { char data; node* next; }; // Function to add a new node to the Linked List node* add(char data) { node* newnode = new node; newnode->data = data; newnode->next = NULL; return newnode; } // Function to convert the string to Linked List. node* string_to_SLL(string text, node* head) { head = add(text[0]); node* curr = head; // curr pointer points to the current node // where the insertion should take place for (int i = 1; i < text.size(); i++) { curr->next = add(text[i]); curr = curr->next; } return head; } // Function to print the data present in all the nodes void print(node* head) { node* curr = head; while (curr != NULL) { cout << curr->data << " -> "; curr = curr->next; } } // Driver code int main() { string text = "GEEKS"; node* head = NULL; head = string_to_SLL(text, head); print(head); return 0; } // This code is contributed by code_freak Java // Java program to Convert a String // to a Singly Linked List class GFG { // Structure for a Singly Linked List static class node { char data; node next; }; // Function to add a new node to the Linked List static node add(char data) { node newnode = new node(); newnode.data = data; newnode.next = null; return newnode; } // Function to convert the string // to Linked List. static node string_to_SLL(String text, node head) { head = add(text.charAt(0)); node curr = head; // curr pointer points to the current node // where the insertion should take place for (int i = 1; i < text.length(); i++) { curr.next = add(text.charAt(i)); curr = curr.next; } return head; } // Function to print the data // present in all the nodes static void print(node head) { node curr = head; while (curr != null) { System.out.print(curr.data + " -> "); curr = curr.next; } } // Driver code public static void main(String[] args) { String text = "GEEKS"; node head = null; head = string_to_SLL(text, head); print(head); } } // This code is contributed by PrinciRaj1992 Python3 # Python3 program to Convert a String # to a Singly Linked List # Structure for a Singly Linked List class node: def __init__(self): data = None next = None # Function to add a node to the Linked List def add(data): newnode = node() newnode.data = data newnode.next = None return newnode # Function to convert the string # to Linked List. def string_to_SLL(text,head): head = add(text[0]) curr = head # curr pointer points to the current node # where the insertion should take place for i in range(len(text) - 1): curr.next = add(text[i + 1]) curr = curr.next return head # Function to print the data # present in all the nodes def print_(head): curr = head while (curr != None) : print ((curr.data), end = " - > " ) curr = curr.next # Driver code text = "GEEKS" head = None head = string_to_SLL(text, head) print_(head) # This code is contributed by Arnab Kundu C# // C# program to Convert a String // to a Singly Linked List using System; class GFG { // Structure for a Singly Linked List class node { public char data; public node next; }; // Function to add a new node // to the Linked List static node add(char data) { node newnode = new node(); newnode.data = data; newnode.next = null; return newnode; } // Function to convert the string // to Linked List. static node string_to_SLL(String text, node head) { head = add(text[0]); node curr = head; // curr pointer points to the current node // where the insertion should take place for (int i = 1; i < text.Length; i++) { curr.next = add(text[i]); curr = curr.next; } return head; } // Function to print the data // present in all the nodes static void print(node head) { node curr = head; while (curr != null) { Console.Write(curr.data + " -> "); curr = curr.next; } } // Driver code public static void Main(String[] args) { String text = "GEEKS"; node head = null; head = string_to_SLL(text, head); print(head); } } // This code is contributed by 29AjayKumar JavaScript <script> // Javascript program to Convert a String // to a Singly Linked List class node { constructor() { this.data=''; this.next=null; } } // Function to add a new node to the Linked List function add(data) { let newnode = new node(); newnode.data = data; newnode.next = null; return newnode; } // Function to convert the string // to Linked List. function string_to_SLL(text,head) { head = add(text[0]); let curr = head; // curr pointer points to the current node // where the insertion should take place for (let i = 1; i < text.length; i++) { curr.next = add(text[i]); curr = curr.next; } return head; } // Function to print the data // present in all the nodes function print(head) { let curr = head; while (curr != null) { document.write(curr.data + " -> "); curr = curr.next; } } // Driver code let text = "GEEKS"; let head = null; head = string_to_SLL(text, head); print(head); // This code is contributed by avanitrachhadiya2155 </script> Output: G -> E -> E -> K -> S -> Time Complexity: O(n)Auxiliary Space: O(n), where n is the length of the given string. Comment More infoAdvertise with us Next Article Convert a String to a Singly Linked List C code_freak Follow Improve Article Tags : Strings Algorithms Technical Scripter DSA Technical Scripter 2019 Data Structures-Linked List +2 More Practice Tags : AlgorithmsStrings Similar Reads Convert a Singly Linked List to an array Given a singly linked list and the task is to convert it into an array.Examples: Input: List = 1 -> 2 -> 3 -> 4 -> NULL Output: 1 2 3 4Input: List = 10 -> 20 -> 30 -> 40 -> 50 -> NULL Output: 10 20 30 40 50 Approach:The idea is to iterate through the linked list and for ea 5 min read Convert Singly Linked List to XOR Linked List Prerequisite: XOR Linked List â A Memory Efficient Doubly Linked List | Set 1XOR Linked List â A Memory Efficient Doubly Linked List | Set 2 An XOR linked list is a memory efficient doubly linked list in which the next pointer of every node stores the XOR of previous and next node's address. Given a 9 min read Convert List of Characters to String in Java Given a list of characters. In this article, we will write a Java program to convert the given list to a string. Example of List-to-String ConversionInput : list = {'g', 'e', 'e', 'k', 's'} Output : "geeks" Input : list = {'a', 'b', 'c'} Output : "abc" Strings - Strings in Java are objects that are 4 min read Singly Linked List Tutorial A singly linked list is a fundamental data structure, it consists of nodes where each node contains a data field and a reference to the next node in the linked list. The next of the last node is null, indicating the end of the list. Linked Lists support efficient insertion and deletion operations.Un 8 min read Check if a linked list of strings forms a palindrome Given a linked list handling string data, check to see whether data is palindrome or not? For example, Input : a -> bc -> d -> dcb -> a -> NULL Output : True String "abcddcba" is palindrome. Output : a -> bc -> d -> ba -> NULL Output : False String "abcdba" is not palindro 6 min read Like