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 DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on 7 min read Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s 12 min read Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge 14 min read Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir 8 min read Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st 2 min read Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta 15+ min read Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc 15 min read Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T 9 min read Dijkstra's Algorithm to find Shortest Paths from a Source to all Given a weighted undirected graph represented as an edge list and a source vertex src, find the shortest path distances from the source vertex to all other vertices in the graph. The graph contains V vertices, numbered from 0 to V - 1.Note: The given graph does not contain any negative edge. Example 12 min read Selection Sort Selection Sort is a comparison-based sorting algorithm. It sorts an array by repeatedly selecting the smallest (or largest) element from the unsorted portion and swapping it with the first unsorted element. This process continues until the entire array is sorted.First we find the smallest element an 8 min read Like