-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_2.java
executable file
·62 lines (56 loc) · 1.4 KB
/
sort_2.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
54
55
56
57
58
59
60
61
62
import java.util.LinkedList;
import java.util.Stack;
class ListNode{
ListNode next;
int val;
ListNode(int val){this.val=val;}
}
public class sort_2 {
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode c1 = l1;
ListNode c2 = l2;
ListNode sentinel = new ListNode(0);
ListNode d = sentinel;
int sum = 0;
while (c1 != null || c2 != null) {
sum /= 10;
if (c1 != null) {
sum += c1.val;
c1 = c1.next;
}
if (c2 != null) {
sum += c2.val;
c2 = c2.next;
}
d.next = new ListNode(sum % 10);
d = d.next;
}
if (sum / 10 == 1)
d.next = new ListNode(1);
return sentinel.next;
}
ListNode first;
public sort_2(){
this.first=null;
}
public void addnode(int data){
ListNode node=new ListNode(data);
node.next=first;
first=node;
}
public static void main(String[] args) {
sort_2 listnode1 = new sort_2();
listnode1.addnode(2);
listnode1.addnode(4);
listnode1.addnode(3);
sort_2 listnode2 = new sort_2();
listnode2.addnode(5);
listnode2.addnode(6);
listnode2.addnode(4);
ListNode node = addTwoNumbers(listnode1.first, listnode2.first);
while (node != null) {
System.out.println(node.val);
node = node.next;
}
}
}