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 -> 4)
Output: 7 -> 0 -> 8

题目说有两个链表分别存储了两个非负整数,链表把数是倒着存的,链表的每个节点只存放一位数字,让你把这两个数加起来然后以题目中的链表形式返回。

举个例子:有两个非负整数342和465,加起来是807,那么在链表中就是2->4->3,5->6->4,返回的807的链表应该是7->0->8。

这个题思路上没难度,就是遍历两个链表,数位分别相加,唯一要注意的地方就是两个数字相加大于10要进位的问题。

代码如下:

 1 class Solution{
 2 public:
 3     ListNode* addToNumbers(ListNode* l1, ListNode* l2){
 4         ListNode* result_list = new ListNode(0);
 5         ListNode* cursor_pointer = result_list;
 6         int carry_num = 0;
 7         while (true){
 8             if (l1 != NULL){
 9                 carry_num += l1->val;
10                 l1 = l1->next;
11             }
12             if (l2 != NULL){
13                 carry_num += l2->val;
14                 l2 = l2->next;
15             }
16             cursor_pointer->val = carry_num % 10;
17             carry_num /= 10;
18             if (l1 != NULL || l2 != NULL || carry_num > 0){
19                 cursor_pointer = (cursor_pointer->next = new ListNode(0));
20             }
21             else{
22                 break;
23             }
24         }
25         return result_list;
26     }
27 };
时间: 2024-10-07 04:13:45

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

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

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

problem 2: add two numbers

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 10 void addtolist(int val, int& add, ListNode*&head, ListNode*&pre) 11 { 12 int sum =