- 浏览: 173158 次
- 性别:
- 来自: 济南
文章分类
最新评论
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
题目的意思是给定两个单向链表,链表中每个节点都存这一位数,将两个链表相加,返回一个新的链表。
解决这道题我们主要是要处理进位,用一个变量carry来维护进位,遍历两个链表,因为要返回一个新链表,因此我们要保存头结点,我们用一个辅助节点helper来保存头结点。代码如下:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
题目的意思是给定两个单向链表,链表中每个节点都存这一位数,将两个链表相加,返回一个新的链表。
解决这道题我们主要是要处理进位,用一个变量carry来维护进位,遍历两个链表,因为要返回一个新链表,因此我们要保存头结点,我们用一个辅助节点helper来保存头结点。代码如下:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int carry = 0; ListNode helper = new ListNode(0); ListNode head = helper; while(l1 != null || l2 != null) { if(l1 != null) { carry += l1.val; l1 = l1.next; } if(l2 != null) { carry += l2.val; l2 = l2.next; } head.next = new ListNode(carry % 10); head = head.next; carry /= 10; } if(carry == 1) { head.next = new ListNode(carry); } return helper.next; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 224Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 222You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 339Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 330Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 448Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 508Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 427Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 617Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 425The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 383Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 514Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 528Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 366All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 858Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 879Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 552Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 598Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 761Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 731You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 626For a undirected graph with tre ...
相关推荐
You are given two non-empty linked lists ... 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. java AC版本
leetcode:Add Two Numbers(java)
手绘算法力扣 2 两数相加(Add Two Numbers)
python python_leetcode面试题解之两数相加AddTwoNumbers
自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;...
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. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...
js代码-2. Add Two Numbers
leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;
leetcode 2 和 c 2021-LeetCode-02_Add_...addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? { guard l1 != nil && l2 != nil else { return nil } var resultTail = ListNode() let resultHead = resu
# Definition for singly-linked list. class ListNode: def __init__(self, x): self.val = x self.next = None #Use listnode to get a decimal number class GetNum: snode = None num = 0 ...
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. Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 ...
2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to Integer 15. 3Sum ...
Add two numbers without using arithmetic operators Chapter 10. Given an array of integers where all the numbers are appearing twice find the only number which appears once Chapter 11. Given an array ...
挑战:01-addOne 难度:基本Swift的: 编写一个名... 例子: addTwoNumbers(5,10)-> 15 addTwoNumbers(10,-2)-> 8 addTwoNumbers(0,0)-> 0 addTwoNumbers('Hello',5)-> NaN function addTwoNumbers ( num1
421 | [Maximum XOR of Two Numbers in an Array](https://leetcode.com/problems/maximum-xor-of-two-numbers-in-an-array/) | [C++](./C++/maximum-xor-of-two-numbers-in-an-array.cpp) [Python](./Python/...
add two numbers in java
Java Program To Add Two Numbers
节点jsat ... * @deprecated addTwoNumbers() will be removed in v2.0.0; please use add() */ exports . addTwoNumbers ( firstNumber , secondNumber ) { return firstNumber + secondNumber ; }