Lintcode: Add Two Numbers

C++

 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     /**
12      * @param l1: the first list
13      * @param l2: the second list
14      * @return: the sum list of l1 and l2
15      */
16     ListNode *addLists(ListNode *l1, ListNode *l2) {
17         // write your code here
18         if (l1 == NULL) {
19             return l2;
20         }
21         if (l2 == NULL) {
22             return l1;
23         }
24         ListNode *ptr1 = l1, *ptr2 = l2;
25         ListNode *result = new ListNode(-1);
26         ListNode *pre = result;
27         int carry = 0, val = 0;
28         while (ptr1 != NULL || ptr2 != NULL) {
29             int val1 = ptr1 == NULL?0:ptr1->val;
30             int val2 = ptr2 == NULL?0:ptr2->val;
31             int sum = val1 + val2 + carry;
32             val = sum;
33             carry = sum/10;
34             pre->next = new ListNode(val);
35             pre = pre->next;
36             if (ptr1!=NULL)
37             {
38                 ptr1 = ptr1->next;
39             }
40             if (ptr2!=NULL)
41             {
42                 ptr2 = ptr2->next;
43             }
44         }
45         if (carry == 1) {
46             pre->next = new ListNode(1);
47         }
48         return result->next;
49     }
50 };
时间: 2024-10-21 14:22:20

Lintcode: Add Two Numbers的相关文章

[Lintcode] Add Two Numbers I && II

Add Two Numbers You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1's digit is at the head of the list. Write a function that adds the two numbers and ret

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: 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:

LeetCode --- 2. Add Two Numbers

题目链接: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

【LeetCode】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 ->

Add Two Numbers(Linked List)

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) + (

Add Two Numbers

题目: 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. 程序设计 链表节点定义 Def

Leetcode 02 Add Two Numbers

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) + (

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions 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 numb

LeetCode: 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 ->