[Leetcode 2, Medium] Add Two Numbers

Problem:

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

Analysis:

The first part of this problem is to use two pointers to trasversal all nodes of two lists. Set one to be the head of the first linked-list, and the second one to be the head of the second linked-list. Move them in the same pace until the next pointer of one of the nodes to be NULL. If the next pointer of the node of the linked-list is not NULL, truncate the reminder part of the linked-list and attach it to the first list.

The second part ot the solution is the simulation of addition. Use an int variable to take the carry of the sum of the current two nodes. After the first loop, we should continue the computation until the carry to be 0 or a new node is added to the list with value 1.

Code:

C++ (36 ms):

 1     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
 2         if(l1 == NULL)
 3             return l2;
 4
 5         if(l2 == NULL)
 6             return l1;
 7
 8         ListNode *p_cur_1 = l1;
 9         ListNode *p_cur_2 = l2;
10         int carry = 0;
11         for(;; p_cur_1 = p_cur_1->next, p_cur_2 = p_cur_2->next)
12         {
13             p_cur_1->val += p_cur_2->val + carry;
14             if(p_cur_1->val >= 10) {
15                 p_cur_1->val %= 10;
16                 carry = 1;
17             } else
18                 carry = 0;
19
20             if(p_cur_1->next == NULL || p_cur_2->next == NULL)
21                 break;
22         }
23
24         if(p_cur_1->next == NULL && p_cur_2->next != NULL) {
25             p_cur_1->next = p_cur_2->next;
26             p_cur_2->next = NULL;
27         }
28
29         while(carry) {
30             if(p_cur_1->next == NULL)
31                 p_cur_1->next = new ListNode(0);
32
33             p_cur_1->next->val += carry;
34             if(p_cur_1->next->val >= 10) {
35                 p_cur_1->next->val %= 10;
36                 carry = 1;
37             } else
38                 carry = 0;
39
40             p_cur_1 = p_cur_1->next;
41         }
42
43         return l1;
44     }
时间: 2024-08-24 13:34:08

[Leetcode 2, Medium] Add Two Numbers的相关文章

【LeetCode】002 Add Two Numbers

题目:LeetCode 002 Add Two Numbers 题意:给定表达非负数的两个链表,这些数字按照反向顺序存储,每个节点包含一个单独的数字,将这两个数相加,返回一个新链表. 样例: Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 链表每个节点的结构: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 ListNode(int x) : val(

LeetCode OJ #2 Add Two Numbers

https://leetcode.com/problems/add-two-numbers/ 题意:两个链表表示两个数 , 2 4 3 5 6 4 按照加法的规则从左往右加以及 进位. 输出:和的链表. 仍然是水题.一些特殊情况需要考虑(这些做题的时候我考虑到了) 1.如果两个长度不等,如l1>l2  ,那么需要l1比l2多出来的部分加上进位copy到ans里 2.5+5的情况,虽然长度相等,但是最高位进位了 可惜,WA了两发,因为 正确的应该是 int v = l1->val + l2-&g

LeetCode No.2 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 ->

【Leetcode】2. Add Two Numbers

Problem: 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 tw

练习编程之leetcode篇----------(2)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 ->

LeetCode Problem 2.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 ->

【Leetcode】445. Add Two Numbers II

You are given two non-empty linked lists representing two non-negative integers. The most significant digit comes first 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

【Leetcode】2. Add Two Numbers 两数相加

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

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