LeetCode002 Add Two Numbers C语言

简介:
1
2
3
4
5
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
 
Subscribe to see which companies asked this question

题意:实际上就是342+465=807然后倒序输出。【一开始也没有看明白。】

参考别人的。一开始题意都没看明白。。。。。。

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
/**
  * Definition for singly-linked list.
  * struct ListNode {
  *     int val;
  *     struct ListNode *next;
  * };
  */
struct  ListNode* addTwoNumbers( struct  ListNode* l1,  struct  ListNode* l2) {
     struct  ListNode *l3 = ( struct  ListNode *) malloc ( sizeof ( struct  ListNode));
     l3->val=0;
     l3->next=NULL;
     //这边因为l3是一直往后遍历的,所以返回头的话要加个head指示一下。
     struct  ListNode* head = l3;
     l3->val=l1->val+l2->val;
     //while循环判断是否申请新的节点
     l1=l1->next;
     l2=l2->next;
     while (l1||l2||l3->val>9){
         l3->next=( struct  ListNode *) malloc ( sizeof ( struct  ListNode));
         l3->next->val=0;
         l3->next->next=NULL;
         if (l1){
             l3->next->val+=l1->val;
             l1=l1->next;
         }
         if (l2){
             l3->next->val+=l2->val;
             l2=l2->next;
         }
         if (l3->val>9){
             l3->next->val+=l3->val/10;
             l3->val=l3->val%10;
         }
         l3=l3->next;
     }
     return  head;
}

PS:主要是搞清楚有几种情况。9+8这种需要申请节点;12+9这种不等长的;

注意C语言链表的使用。。。

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
/**
  * Definition for singly-linked list.
  * public class ListNode {
  *     int val;
  *     ListNode next;
  *     ListNode(int x) { val = x; }
  * }
  */
public  class  Solution {
     public  ListNode addTwoNumbers(ListNode l1, ListNode l2) {
         ////直接模拟加法就行。先申请一个head
         ListNode head= new  ListNode( 0 );
         int  carry= 0 ;
         ListNode p1=l1,p2=l2,p3=head;
         while (l1!= null  || l2!= null ){
             if (l1!= null ){
                 carry+=l1.val;
                 l1=l1.next;
             }
             if (l2!= null ){
                 carry+=l2.val;
                 l2=l2.next;
             }
             p3.next= new  ListNode(carry% 10 );
             p3=p3.next;
             carry=carry/ 10 ;
         }
         if (carry== 1 ){
             p3.next= new  ListNode( 1 );
         }
         return  head.next; 
     }
}

JAVA版本的。


本文转自 努力的C 51CTO博客,原文链接:http://blog.51cto.com/fulin0532/1864621


相关文章
|
6月前
Leetcode 623. Add One Row to Tree
题目很简单,在树的第d层加一层,值为v。递归增加一层就好了。代码如下
25 0
|
8月前
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
11月前
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode 1380. 矩阵中的幸运数 Lucky Numbers in a Matrix
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode Contest 178-1365. 有多少小于当前数字的数字 How Many Numbers Are Smaller Than the Current Number
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 5340. 统计有序矩阵中的负数 Count Negative Numbers in a Sorted Matrix
LeetCode 415. Add Strings
给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
66 0
LeetCode 415. Add Strings
LeetCode 357. Count Numbers with Unique Digits
给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。
63 0
LeetCode 357. Count Numbers with Unique Digits
|
存储 Python
LeetCode 315. Count of Smaller Numbers After Self
给定一个整数数组 nums,按要求返回一个新数组 counts。数组 counts 有该性质: counts[i] 的值是 nums[i] 右侧小于 nums[i] 的元素的数量。
74 0
LeetCode 315. Count of Smaller Numbers After Self
LeetCode 258. Add Digits
给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。
52 0
LeetCode 258. Add Digits