码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode 2. Add Two Numbers

时间:2019-05-23 00:02:42      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:its   ini   input   one   store   tput   fir   linked   while   

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 class Solution {
10     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
11         ListNode first = new ListNode(-1);//指向结果list的头结点
12         ListNode iter = first;
13         int addOne = 0;
14 
15         while (l1.val < 0) l1 = l1.next;
16         while (l2.val < 0) l2 = l2.next;
17 
18         while (l1 != null && l2 != null) {
19             int val = l1.val + l2.val + addOne;
20             addOne = val / 10;//相加的进位
21             val %= 10;
22             ListNode listNode = new ListNode(val);
23             listNode.next = null;
24             iter.next = listNode;
25 
26             l1 = l1.next;
27             l2 = l2.next;
28             iter = iter.next;
29         }
30         if (addOne != 0 && l1 == null && l2 == null){
31             ListNode listNode = new ListNode(addOne);
32             listNode.next = null;
33             iter.next = listNode;
34         }
35 
36         if (l1 != null) {
37             l1.val += addOne;
38             iter.next = l1;
39             while (l1.val / 10 > 0 && l1 != null) {
40                 addOne = l1.val / 10;
41                 l1.val %= 10;
42                 if (l1.next == null){
43                     ListNode listNode = new ListNode(addOne);
44                     listNode.next = null;
45                     l1.next = listNode;
46                     break;
47                 }
48                 l1 = l1.next;
49                 l1.val += addOne;
50             }
51         }
52 
53         if (l2 != null) {
54             l2.val += addOne;
55             iter.next = l2;
56 
57             while (l2.val / 10 > 0 && l2 != null) {
58                 addOne = l2.val / 10;
59                 l2.val %= 10;
60                 if (l2.next == null){
61                     ListNode listNode = new ListNode(addOne);
62                     listNode.next = null;
63                     l2.next = listNode;
64                     break;
65                 }
66                 l2 = l2.next;
67                 l2.val += addOne;
68             }
69         }
70 
71         // 输出结果
72 //        while (first != null) {
73 //            System.out.print(first.val + "->");
74 //            first = first.next;
75 //        }
76         return first.next;//null
77     }
78 }

 

LeetCode 2. Add Two Numbers

标签:its   ini   input   one   store   tput   fir   linked   while   

原文地址:https://www.cnblogs.com/yfs123456/p/10909168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!