-
Notifications
You must be signed in to change notification settings - Fork 0
/
String_To_LinkedList.java
53 lines (53 loc) · 1.27 KB
/
String_To_LinkedList.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import java.util.Scanner;
public class String_To_LinkedList
{
static class Node
{
char data;
Node next;
Node(char val)
{
data = val;
next = null;
}
}
static Node add(char data)
{
Node newnode = new Node(data);
newnode.data = data;
newnode.next = null;
return newnode;
}
static Node addCharacters(String name, Node head,int len)
{
head = add(name.charAt(0));
Node curr = head;
for (int index=1;index<len;index++)
{
curr.next=add(name.charAt(index));
curr=curr.next;
}
return head;
}
static void print(Node head)
{
Node curr = head;
while (curr != null)
{
System.out.print(curr.data);
System.out.print("->");
curr = curr.next;
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String name=sc.next();
int len=name.length();
Node head = null;
head = addCharacters(name,head,len);
System.out.println("Created List Contians : \n");
print(head);
sc.close();
}
}