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 it into a new node in the Linked List
- Print the Linked List
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
struct node {
char data;
node* next;
};
node* add( char data)
{
node* newnode = new node;
newnode->data = data;
newnode->next = NULL;
return newnode;
}
node* string_to_SLL(string text, node* head)
{
head = add(text[0]);
node* curr = head;
for ( int i = 1; i < text.size(); i++) {
curr->next = add(text[i]);
curr = curr->next;
}
return head;
}
void print(node* head)
{
node* curr = head;
while (curr != NULL) {
cout << curr->data << " -> " ;
curr = curr->next;
}
}
int main()
{
string text = "GEEKS" ;
node* head = NULL;
head = string_to_SLL(text, head);
print(head);
return 0;
}
|
Java
class GFG
{
static class node
{
char data;
node next;
};
static node add( char data)
{
node newnode = new node();
newnode.data = data;
newnode.next = null ;
return newnode;
}
static node string_to_SLL(String text,
node head)
{
head = add(text.charAt( 0 ));
node curr = head;
for ( int i = 1 ; i < text.length(); i++)
{
curr.next = add(text.charAt(i));
curr = curr.next;
}
return head;
}
static void print(node head)
{
node curr = head;
while (curr != null )
{
System.out.print(curr.data + " -> " );
curr = curr.next;
}
}
public static void main(String[] args)
{
String text = "GEEKS" ;
node head = null ;
head = string_to_SLL(text, head);
print(head);
}
}
|
Python3
class node:
def __init__( self ):
data = None
next = None
def add(data):
newnode = node()
newnode.data = data
newnode. next = None
return newnode
def string_to_SLL(text,head):
head = add(text[ 0 ])
curr = head
for i in range ( len (text) - 1 ):
curr. next = add(text[i + 1 ])
curr = curr. next
return head
def print_(head):
curr = head
while (curr ! = None ) :
print ((curr.data), end = " - > " )
curr = curr. next
text = "GEEKS"
head = None
head = string_to_SLL(text, head)
print_(head)
|
C#
using System;
class GFG
{
class node
{
public char data;
public node next;
};
static node add( char data)
{
node newnode = new node();
newnode.data = data;
newnode.next = null ;
return newnode;
}
static node string_to_SLL(String text,
node head)
{
head = add(text[0]);
node curr = head;
for ( int i = 1; i < text.Length; i++)
{
curr.next = add(text[i]);
curr = curr.next;
}
return head;
}
static void print(node head)
{
node curr = head;
while (curr != null )
{
Console.Write(curr.data + " -> " );
curr = curr.next;
}
}
public static void Main(String[] args)
{
String text = "GEEKS" ;
node head = null ;
head = string_to_SLL(text, head);
print(head);
}
}
|
Javascript
<script>
class node
{
constructor()
{
this .data= '' ;
this .next= null ;
}
}
function add(data)
{
let newnode = new node();
newnode.data = data;
newnode.next = null ;
return newnode;
}
function string_to_SLL(text,head)
{
head = add(text[0]);
let curr = head;
for (let i = 1; i < text.length; i++)
{
curr.next = add(text[i]);
curr = curr.next;
}
return head;
}
function print(head)
{
let curr = head;
while (curr != null )
{
document.write(curr.data + " -> " );
curr = curr.next;
}
}
let text = "GEEKS" ;
let head = null ;
head = string_to_SLL(text, head);
print(head);
</script>
|
Output:
G -> E -> E -> K -> S ->
Time Complexity: O(n)
Auxiliary Space: O(n), where n is the length of the given string.
Last Updated :
22 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...