mirror of
https://github.com/Xevion/leetcode.git
synced 2025-12-06 05:15:30 -06:00
add-two-numbers solution java
This commit is contained in:
40
add-two-numbers/Solution.java
Normal file
40
add-two-numbers/Solution.java
Normal file
@@ -0,0 +1,40 @@
|
||||
// Accepted
|
||||
// Runtime: 1 ms
|
||||
// Memory Usage: 39.3 MB
|
||||
// Submitted: January 15th, 2021
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
public ListNode addTwoNumbers(ListNode a, ListNode b) {
|
||||
ListNode dummy = new ListNode(0);
|
||||
ListNode cur = dummy;
|
||||
int carry = 0;
|
||||
|
||||
while (a != null || b != null) {
|
||||
int x = (a != null) ? a.val : 0;
|
||||
int y = (b != null) ? b.val : 0;
|
||||
int sum = carry + x + y;
|
||||
|
||||
carry = sum / 10;
|
||||
cur.next = new ListNode(sum % 10);
|
||||
cur = cur.next;
|
||||
|
||||
if (a != null) a = a.next;
|
||||
if (b != null) b = b.next;
|
||||
}
|
||||
|
||||
if (carry > 0)
|
||||
cur.next = new ListNode(carry);
|
||||
|
||||
return dummy.next;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user