[leetcode]_Add Two Numbers

题目:两个链表存储数字,然后求和,和值存储在一个链表中。

代码:


 1 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
2 ListNode head = new ListNode(0);
3 ListNode result = head;
4
5 int carry = 0 , tempSum = 0;
6 while(l1 != null || l2 != null){
7 int v1 , v2;
8 v1 = (l1 != null) ? l1.val : 0;
9 v2 = (l2 != null) ? l2.val : 0;
10 tempSum = v1 + v2 + carry;
11 carry = tempSum / 10;
12 tempSum = tempSum % 10;
13 head.next = new ListNode(tempSum);
14 head = head.next;
15
16 l1 = (l1 != null) ? l1.next : null;
17 l2 = (l2 != null) ? l2.next : null;
18 }
19
20 if(carry > 0) head.next = new ListNode(carry);
21
22 return result.next;
23 }

[leetcode]_Add Two Numbers,布布扣,bubuko.com

时间: 2024-08-21 13:09:43

[leetcode]_Add Two Numbers的相关文章

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

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

leetcode——Add Two Numbers 两个链表表示的正整数对其求和(AC)

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:Consecutive Numbers - 找出连续出现的数字

1.题目名称 Consecutive Numbers(找出连续出现的数字) 2.题目地址 https://leetcode.com/problems/consecutive-numbers/ 3.题目内容 写一个SQL,查出表Logs中连续出现至少3次的数字: +----+-----+ | Id | Num | +----+-----+ | 1  |  1  | | 2  |  1  | | 3  |  1  | | 4  |  2  | | 5  |  1  | | 6  |  2  | | 

[LeetCode] Valid Phone Numbers 验证电话号码

Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers. You may assume that a valid phone number must appear in one of the following two formats: (xxx) xxx-xxxx or

【Leetcode】Count Numbers with Unique Digits

题目链接:https://leetcode.com/problems/count-numbers-with-unique-digits/ 题目: Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Example: Given n = 2, return 91. (The answer should be the total numbers in the range

LeetCode: Add Two Numbers 解题报告

Add Two NumbersYou 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】Consecutive Numbers

题目链接:https://leetcode.com/problems/consecutive-numbers/ 题目: Write a SQL query to find all numbers that appear at least three times consecutively. +--+-–+ | Id | Num | +--+-–+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +--+-