Convert a String to a Singly Linked List
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 List
- Fetch each character of the string and insert into a new node in the Linked List
- Print 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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
Output:
G -> E -> E -> K -> S ->
Recommended Posts:
- Convert a Singly Linked List to an array
- Sum of the nodes of a Singly Linked List
- Implement a stack using singly linked list
- Delete all Non-Prime Nodes from a Singly Linked List
- Sum and Product of the nodes of a Singly Linked List which are divisible by K
- Delete all Prime Nodes from a Singly Linked List
- Sum and Product of all Prime Nodes of a Singly Linked List
- Reverse a singly Linked List in groups of given size | Set 3
- Find the common nodes in two singly linked list
- Difference between a Static Queue and a Singly Linked List
- Sum and Product of the nodes of a Circular Singly Linked List which are divisible by K
- Minimum and Maximum Prime Numbers of a Singly Linked List
- Delete all Prime Nodes from a Circular Singly Linked List
- Find minimum and maximum elements in singly Circular Linked List
- Convert List of Characters to String in Java
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.


