-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubled.java
More file actions
48 lines (48 loc) · 929 Bytes
/
Doubled.java
File metadata and controls
48 lines (48 loc) · 929 Bytes
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
import java.util.*;
import java.util.LinkedList;
class Node4
{
int data;
Node4 next;
Node4(int d)
{
data=d;
}
}
public class Doubled
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
String inp[]=in.nextLine().split(",");
Node4 head=null,tail=null;
for(String s:inp)
{
int digit=Integer.parseInt(s.trim());
Node4 node=new Node4(digit);
if(head==null)
head=tail=node;
else{
tail.next=node;
tail=node;
}
}
Stack<Integer> st=new Stack<>();
while(head!=null)
{
st.push(head.data);
head=head.next;
}
int c=0;
LinkedList<Integer> res=new LinkedList<>();
while(!st.isEmpty())
{
int s=st.pop()*2+c;
res.addFirst(s%10);
c=s/10;
}
if(c>0)
res.addFirst(c);
System.out.println(res);
}
}