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

Add two Numbers

时间:2015-04-16 19:34:28      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

Add Two Numbers

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

比较简单,还是用Java编写,一次过O(∩_∩)O

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) {
 7  *         val = x;
 8  *         next = null;
 9  *     }
10  * }
11  */
12 public class Solution {
13     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
14         int s = l1.val+l2.val,carry = 0;
15         if(s>9)
16         {
17             s=s-10;
18             carry=1;
19         }
20         ListNode sum = new ListNode(s);
21         ListNode next= sum;
22         while(l1.next!=null&&l2.next!=null)
23         {
24             s=l1.next.val+l2.next.val+carry;
25             carry=0;
26             if(s>9)
27             {
28                 s=s-10;
29                 carry=1;
30             }
31             next.next=new ListNode(s);
32             next=next.next;
33             l1=l1.next;
34             l2=l2.next;
35         }
36         while(l1.next==null&&l2.next!=null)
37         {
38             s=l2.next.val+carry;
39             carry=0;
40             if(s>9)
41             {
42                 s=s-10;
43                 carry=1;
44             }
45             next.next=new ListNode(s);
46             next=next.next;
47             l2=l2.next;
48         }
49         while(l1.next!=null&&l2.next==null)
50         {
51             s=l1.next.val+carry;
52             carry=0;
53             if(s>9)
54             {
55                 s=s-10;
56                 carry=1;
57             }
58             next.next=new ListNode(s);
59             next=next.next;
60             l1=l1.next;
61         }
62         if(carry==1)
63             next.next=new ListNode(carry);
64         
65         return sum;
66     }
67 }

运行时间排最后。。因为最近在学Java。查了一下其他解决办法。下面那个是445ms的,比我的快了1ms,虽然时间差不多,不过看着就比我那个好多了。╮(╯▽╰)╭技术分享

 1 public class Solution {
 2     public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
 3        int carry =0;
 4  
 5         ListNode newHead = new ListNode(0);
 6         ListNode p1 = l1, p2 = l2, p3=newHead;
 7  
 8         while(p1 != null || p2 != null){
 9             if(p1 != null){
10                 carry += p1.val;
11                 p1 = p1.next;
12             }
13  
14             if(p2 != null){
15                 carry += p2.val;
16                 p2 = p2.next;
17             }
18  
19             p3.next = new ListNode(carry%10);
20             p3 = p3.next;
21             carry /= 10;
22         }
23  
24         if(carry==1) 
25             p3.next=new ListNode(1);
26  
27         return newHead.next;
28     }
29 }

 

 


参考来源:http://www.programcreek.com/2012/12/add-two-numbers/

 

Add two Numbers

标签:

原文地址:http://www.cnblogs.com/zhouyee/p/4432697.html

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