-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq002_AddTwoNumbers.java
More file actions
75 lines (59 loc) · 1.75 KB
/
q002_AddTwoNumbers.java
File metadata and controls
75 lines (59 loc) · 1.75 KB
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
70
71
72
73
package leetcode_algorithm;
/**
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
*/
public class q002_AddTwoNumbers {
public static void main(String[] args) {
ListNode a1 = new ListNode(2);
ListNode a2 = new ListNode(4);
ListNode a3 = new ListNode(3);
a1.next = a2;
a2.next = a3;
ListNode b1 = new ListNode(5);
ListNode b2 = new ListNode(6);
ListNode b3 = new ListNode(4);
b1.next = b2;
b2.next = b3;
System.out.println(solution(a1 , a2));
}
/**
* ½â·¨1
*
* @param l1
* @param l2
* @return
*/
public static ListNode solution(ListNode l1 , ListNode l2){
ListNode dummyHead = new ListNode(0);
ListNode p = l1 , q = l2 , curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum/10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if(q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
private static class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
public String toString() {
return val + " " + (next != null ? next : "");
}
}
}