LeetCode Algorithm 02

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

Tags:Linked List, Math

分析:逐位相加,考虑进位。

 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         if (!l1 || !l2) {
13             return NULL;
14         }
15         int carry = 0;  //进位
16         ListNode * result = new ListNode(0);
17         ListNode * first = result;  //追踪结果链表的当前节点
18         ListNode * pre = NULL;  //追踪结果链表的前一个节点
19         //当l1和l2均没有超过链表末尾节点时
20         while (l1 && l2) {
21             first->val = (carry + l1->val + l2->val) % 10;
22             carry = (carry + l1->val + l2->val) / 10;
23             if (pre == NULL)
24                 pre = first;
25             else {
26                 pre->next = first;
27                 pre = first;
28             }
29             first = new ListNode(0);
30             l1 = l1->next;
31             l2 = l2->next;
32         }
33         //当l1和l2都超过链表末尾节点时
34         if (!l1 && !l2) {
35             if (carry == 1) {
36                 first->val = carry;
37                 pre->next = first;
38             }
39             return result;
40         }
41         //当l1超过末尾而l2尚未超过时
42         if (!l1 && l2) {
43             while (l2) {
44                 first->val = (carry + l2->val) % 10;
45                 carry = (carry + l2->val) / 10;
46                 if (pre == NULL)
47                     pre = first;
48                 else {
49                     pre->next = first;
50                     pre = first;
51                 }
52                 first = new ListNode(0);
53                 l2 = l2->next;
54             }
55             if (carry == 1) {
56                 first->val = 1;
57                 pre->next = first;
58             }
59             return result;
60         }
61         //当l2超过末尾而l1尚未超过时
62         if (!l2 && l1) {
63             while (l1) {
64                 first->val = (carry + l1->val) % 10;
65                 carry = (carry + l1->val) / 10;
66                 if (pre == NULL)
67                     pre = first;
68                 else {
69                     pre->next = first;
70                     pre = first;
71                 }
72                 first = new ListNode(0);
73                 l1 = l1->next;
74             }
75             if (carry == 1) {
76                 first->val = 1;
77                 pre->next = first;
78             }
79             return result;
80         }
81
82     }
83 };
时间: 2024-10-13 11:06:44

LeetCode Algorithm 02的相关文章

LeetCode Algorithm 05

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Tags: String 容易想到的思路(可以解但会超时): class Solution { public: string longestPalin

LeetCode Algorithm

原文出处:[LeetCode] 算法参考:[陈皓 coolshell] 3.Longest Substring Without Repeating Characters 3.Longest Substring Without Repeating Characters /********************************************************** ** 3.Longest Substring Without Repeating Characters ** G

Leetcode Algorithm No.241Different Ways to Add Parentheses

题目来自Leetcode Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are +, - and *. Example 1 Input: "2-1-1". ((2-1)-1) = 0 (2-(1-1)

LeetCode Algorithm 04

There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Tags: Divide and Conquer, Array, Binary Search 分析: 对于数字个数k,如果k为奇数,则k个数的中位数为第(k/2+1)个:对

LeetCode Algorithm 07_Reverse Integer

Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throug

LeetCode Algorithm 06

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I RAnd then read line by line: "PAHNAPLSIIG

LeetCode Algorithm 03

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest subst

LeetCode Algorithm 01

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that

C语言 string::size_type类型

string::size_type类型 从逻辑上来讲,size()成员函数似乎应该返回整型数值,或如2.2节"建议"中所述的无符号整数.但事实上,size操作返回的是string::size_type类型的值.我们需要对这种类型做一些解释. string类类型和许多其他库类型都定义了一些伙伴类型(companion types).这些伙伴类型使得库类型的使用是机器无关的(machine-independent).size_type就是这些伙伴类型中的一种.它定义为与unsigned型(