【LeetCode刷题系列 - 002题】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 do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

代码(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     ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
12         // 结果链表的首节点
13         ListNode *head = nullptr;
14         // 尾插法建立单链表指向尾节点
15         ListNode *tail = nullptr;
16         // num1为两整数相加后的个位数值
17         int num1 = 0;
18         // num2为两整数相加后的进位数值
19         int num2 = 0;
20         // node1Val为l1当前节点的数据域或者0
21         int node1Val = 0;
22         // node2Val为l2当前节点的数据域或者0
23         int node2Val = 0;
24         // count变量的设置是为了尾插法建立单链表设置的计数器
25         int count = 0;
26
27         while(l1 != nullptr || l2 != nullptr)
28         {
29             // 如果节点不为空,则取节点的数据域否则让节点的数据域为0
30             if(l1 == nullptr)
31             {
32                 node1Val = 0;
33             }else
34             {
35                 node1Val = l1->val;
36             }
37             if(l2 == nullptr)
38             {
39                 node2Val = 0;
40             }else
41             {
42                  node2Val = l2->val;
43             }
44             // 本次计算结果 = 本次计算的node1Val + 本次计算的node1Va2 + 进位值
45             num1 = node1Val + node2Val + num2;
46             if(num1 >= 10)
47             {
48                 num1 = num1 - 10;
49                 num2 = 1;
50             }else
51             {
52                 num2 = 0;
53             }
54             // 为建立结果链表创建节点
55             ListNode *newNode = new ListNode(num1);
56             // 尾插法建立结果单链表,如果是首节点,需要进行特殊处理
57             if(count == 0)
58             {
59                 head = tail = newNode;
60             }else
61             {
62                 tail->next = newNode;
63                 tail = newNode;
64             }
65             // 链表向前移动并释放原始链表所占的内存空间
66             if(l1 != nullptr)
67             {
68                 ListNode *tempNode1 = l1;
69                 l1 = l1->next;
70                 delete tempNode1;
71             }
72             if(l2 != nullptr)
73             {
74                 ListNode *tempNode2 = l2;
75                 l2 = l2->next;
76                 delete tempNode2;
77             }
78
79             /* 为了解决例如情况:l1 = [5]
80                               l2 = [5]
81                这种情况
82             */
83             if(l1 == nullptr && l2 == nullptr && num2 != 0)
84             {
85                 ListNode *newNode = new ListNode(num2);
86                 tail->next = newNode;
87                 tail = newNode;
88                 return head;
89             }
90             count++;
91
92         }
93
94         return head;
95     }
96 };

原文地址:https://www.cnblogs.com/xuelisheng/p/10770215.html

时间: 2024-10-29 19:13:33

【LeetCode刷题系列 - 002题】Add Two Numbers的相关文章

LeetCode 445. 两数相加 II(Add Two Numbers II)

445. 两数相加 II 445. Add Two Numbers II 题目描述 给定两个非空链表来代表两个非负整数.数字最高位位于链表开始位置.它们的每个节点只存储单个数字.将这两数相加会返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 进阶: 如果输入链表不能修改该如何处理?换句话说,你不能对列表中的节点进行翻转. LeetCode445. Add Two Numbers II中等 示例: 输入: (7 -> 2 -> 4 -> 3) + (5 ->

leetcode速度才是王道 2. Add Two Numbers

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)

【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Exp

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 cont

No.002: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.Example:Input: (2 -> 4 -> 3) + (5 -&g

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

【leetcode刷题笔记】Add Binary

Given two binary strings, return their sum (also a binary string). For example,a = "11"b = "1"Return "100". 题解:简单的二进制加法模拟.a,b的最后以为对齐开始进行加法,用carries保存进位,如果加完后最高位还有进位,那么要在结果的最前面加一个1. 代码如下: 1 public class Solution { 2 public Str

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "b