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

LeetCode 2 Add Two Numbers

时间:2015-07-21 12:53:28      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:leetcode   add two numbers   

You are giventwo linked lists representing two non-negative numbers. The digits are storedin reverse order and each of their nodes contain a single digit. Add the twonumbers and return it as a linked list.

Input: (2 -> 4-> 3) + (5 -> 6 -> 4)

Output: 7 -> 0-> 8


思路:一开始的思路肯定是先分别计算连个链表的值,然后相加得到第三个链表的值,然后在输出为链表形式。但是:::这种方法会超时。

仔细分析,可以看出,可以两个链表分别取第一个数相加,判断是否有溢出位,然后写到输出链表,依次取两个链表剩下的结点值。这样两个链表只用循环一次,然后就可以得到输出链表。这里的难点就是要分析两个数相加sum>=10,则溢出位设为1,且sum = sum % 10;否则溢出位设为0;在除了第一个结点以外的结点相加时,还要加上溢出位。



1.	/** 
2.	 * Definition for singly-linked list. 
3.	 * struct ListNode { 
4.	 *     int val; 
5.	 *     ListNode *next; 
6.	 *     ListNode(int x) : val(x), next(NULL) {} 
7.	 * }; 
8.	 */  
9.	class Solution {  
10.	public:  
11.	    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {  
12.	        int val1,val2,succ;  
13.	        ListNode *ret = new ListNode(0);  
14.	        int sum = l1->val + l2->val;  
15.	        if(sum >= 10)  
16.	        {  
17.	            succ = 1;  
18.	            ret->val = sum % 10;  
19.	        }  
20.	        else  
21.	        {  
22.	            succ = 0;  
23.	            ret->val = sum;  
24.	        }  
25.	        ret->next = NULL;  
26.	          
27.	        ListNode *p = ret;  
28.	        ListNode *q;  
29.	        while(l1->next && l2->next)  
30.	        {  
31.	            l1 = l1->next;  
32.	            l2 = l2->next;  
33.	            sum = l1->val + l2->val + succ;  
34.	            q = new ListNode(0);  
35.	            if(sum >= 10)  
36.	            {  
37.	                succ = 1;  
38.	                q->val = sum % 10;  
39.	            }  
40.	            else  
41.	            {  
42.	                succ = 0;  
43.	                q->val = sum;  
44.	            }  
45.	            p->next = q;  
46.	            p = q;  
47.	  
48.	        }  
49.	        while(l1->next)  
50.	        {  
51.	            l1 = l1->next;  
52.	            sum = l1->val + succ;  
53.	            q = new ListNode(0);  
54.	            if(sum >= 10)  
55.	            {  
56.	                succ = 1;  
57.	                q->val = sum % 10;  
58.	            }  
59.	            else  
60.	            {  
61.	                succ = 0;  
62.	                q->val = sum;  
63.	            }  
64.	            p->next = q;  
65.	            p = q;  
66.	              
67.	        }  
68.	        while(l2->next)  
69.	        {  
70.	            l2 = l2->next;  
71.	            sum = l2->val + succ;  
72.	            q = new ListNode(0);  
73.	            if(sum >= 10)  
74.	            {  
75.	                succ = 1;  
76.	                q->val = sum % 10;  
77.	            }  
78.	            else  
79.	            {  
80.	                succ = 0;  
81.	                q->val = sum;  
82.	            }  
83.	            p->next = q;  
84.	            p = q;  
85.	  
86.	        }   
87.	        while(succ)  
88.	        {  
89.	            q = new ListNode(0);  
90.	            q->val = succ;  
91.	            p->next = q;  
92.	            p = q;  
93.	            succ = 0;  
94.	        }  
95.	        return ret;  
96.	          
97.	/*超时版本 
98.	        int val1,val2,val3; 
99.	        val1 += l1->val; 
100.	        val2 += l2->val; 
101.	        int i = 1; 
102.	        while(l1->next != NULL) 
103.	        { 
104.	            i = i * 10; 
105.	            l1 = l1->next; 
106.	            val1 += i * l1->val; 
107.	        } 
108.	        i = 1; 
109.	        while(l2->next != NULL) 
110.	        { 
111.	            i = i * 10; 
112.	            l1 = l2->next; 
113.	            val2 += i * l2->val; 
114.	        }  
115.	         
116.	        val3 = val1 + val2; 
117.	        ListNode *ret = new ListNode(0); 
118.	        ret->val = val3 % 10; 
119.	        ret->next = NULL; 
120.	        ListNode *tmp1 = ret; 
121.	        ListNode *tmp2; 
122.	        while(val3 / 10) 
123.	        { 
124.	            val3 = val3 / 10; 
125.	            tmp2 = new ListNode(0); 
126.	            tmp2->next = NULL; 
127.	            tmp2->val = val3 % 10; 
128.	            tmp1->next = tmp2; 
129.	        } 
130.	        return ret; 
131.	*/  
132.	    }  
133.	};  


版权声明:本文为博主原创文章,未经博主允许不得转载。

LeetCode 2 Add Two Numbers

标签:leetcode   add two numbers   

原文地址:http://blog.csdn.net/csdnjack_/article/details/46982567

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