-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdd Two Numbers.java
69 lines (60 loc) · 1.57 KB
/
Add Two Numbers.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
63
64
65
66
67
68
69
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
/*
2 -> 4 -> 3
5 -> 6 -> 4
7 0 8
*/
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode fake = new ListNode(0);
ListNode p = fake;
//initialize the nodes of the two lists
ListNode p1 = l1;
ListNode p2 = l2;
int carry = 0;
while(p1!=null || p2!=null){
int sum = carry;
if(p1!=null){
//put the value in the first node into the sum
sum += p1.val;
//change the location of the pointer to the next node
p1 = p1.next;
}
if(p2!=null){
//put the value in the second node into the sum
sum += p2.val;
//change the location of the pointer to the next node
p2 = p2.next;
}
//check if the value of the sum 15
if(sum>9){
//so, carry equals 1
carry=1;
//value of sum equals 5
sum = sum-10;
}else{
carry = 0;
}
//create node to hold the sum
ListNode l = new ListNode(sum);
//determine the pointers
p.next = l;
p = p.next;
}
//don't forget check the carry value at the end
if(carry > 0){
ListNode l = new ListNode(carry);
p.next = l;
}
return fake.next;
}
}