Convert a String to a Singly Linked List - GeeksforGeeks

19 Jul, 2021
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++
Java
Python3
C#
Javascript
// C++ program to Convert a String// to a Singly Linked List #include <iostream>using namespace std; // Structure for a Singly Linked Liststruct node {    char data;    node* next;}; // Function to add a new node to the Linked Listnode* 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 nodesvoid print(node* head){    node* curr = head;    while (curr != NULL) {        cout << curr->data << " -> ";        curr = curr->next;    }} // Driver codeint 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 program to Convert a String// to a Singly Linked Listclass GFG{ // Structure for a Singly Linked Liststatic class node{    char data;    node next;}; // Function to add a new node to the Linked Liststatic 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 nodesstatic void print(node head){    node curr = head;    while (curr != null)    {        System.out.print(curr.data + " -> ");        curr = curr.next;    }} // Driver codepublic 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 program to Convert a String# to a Singly Linked List # Structure for a Singly Linked Listclass node:    def __init__(self):        data = None        next = None # Function to add a node to the Linked Listdef 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 nodesdef print_(head):     curr = head    while (curr != None) :             print ((curr.data), end = " - > " )        curr = curr.next     # Driver codetext = "GEEKS"head = Nonehead = string_to_SLL(text, head)print_(head) # This code is contributed by Arnab Kundu
// C# program to Convert a String// to a Singly Linked Listusing System; class GFG{ // Structure for a Singly Linked Listclass node{    public char data;    public node next;}; // Function to add a new node// to the Linked Liststatic 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 nodesstatic void print(node head){    node curr = head;    while (curr != null)    {        Console.Write(curr.data + " -> ");        curr = curr.next;    }} // Driver codepublic 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
<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 nodesfunction print(head){    let curr = head;    while (curr != null)    {        document.write(curr.data + " -> ");        curr = curr.next;    }} // Driver codelet text = "GEEKS"; let head = null;head = string_to_SLL(text, head); print(head); // This code is contributed by avanitrachhadiya2155</script>
G -> E -> E -> K -> S ->
 
princiraj1992
29AjayKumar
ManasChhabra2
andrew1234
avanitrachhadiya2155
Data Structures-Linked List
Strings
Technical Scripter
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Top 50 String Coding Problems for Interviews
Naive algorithm for Pattern Searching
Vigenère Cipher
Hill Cipher
Count words in a given string
How to Append a Character to a String in C
Convert character array to string in C++
Program to count occurrence of a given character in a string
sprintf() in C
Converting Roman Numerals to Decimal lying between 1 to 3999