`
wangqisen
  • 浏览: 46958 次
文章分类
社区版块
存档分类
最新评论

leetcode之Add Two Numbers

 
阅读更多

Add Two Numbers

AC Rate: 1710/7566
My Submissions

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

此题很简单,不多说。

下面是我的代码:

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
		ListNode node=null;
		node=buildList(l1, l2, node);
		return node;
	}
	
	public ListNode buildList(ListNode l1, ListNode l2,ListNode node){
		int addDigit=0,v1,v2;
		ListNode tmp=node;
		while(!(l1==null&&l2==null)){
			if(l1!=null)
				v1=l1.val;
			else
				v1=0;
			if(l2!=null)
				v2=l2.val;
			else 
				v2=0;
			int value=v1+v2+addDigit;
			int digit=value%10;
			int add=value/10;
			ListNode newNode=new ListNode(digit);
			addDigit=add;
			if(tmp==null){
				tmp=newNode;
				node=tmp;
			}
			else{
				tmp.next=newNode;
				tmp=tmp.next;
			}
			if(l1!=null)
				l1=l1.next;
			if(l2!=null)
				l2=l2.next;
		}
		ListNode tmp2=node;
		if(addDigit!=0){
			while(tmp2.next!=null)
				tmp2=tmp2.next;
			tmp2.next=new ListNode(addDigit);
		}
		return node;
	}


分享到:
评论

相关推荐

    LeetCode2 Add Two Numbers

    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

    自己写的一个完整的程序,包括main函数,在VS上面提交通过,但是放到leetcode上面会出现问题;只是作为一个参考,一起学习学习0.o!解决的问题有:第一:两个链表的最后一个值相加后进位的问题;第二:两个链表的...

    AddTwoNumbers.java

    leetcode:Add Two Numbers(java)

    python-leetcode面试题解之两数相加AddTwoNumbers.zip

    python python_leetcode面试题解之两数相加AddTwoNumbers

    addTwoNumbers_leetcode_

    给你两个?非空 的链表,表示两个非负的整数。它们每位数字都是按照?逆序?的方式存储的,并且每个节点只能存储?一位?数字。请你将两个数相加,并以相同形式返回一个表示和的链表。你可以假设除了数字 0 之外,这两个...

    0-leetcode:leetcode练习:twosum问题;addtwonumbers问题;reverse integer问题;最大不重复子字符串长度问题等,详细见readme.md;

    leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;

    手绘算法力扣 2 两数相加(Add Two Numbers)

    手绘算法力扣 2 两数相加(Add Two Numbers)

    leetcode2sumc-2021-LeetCode-02_Add_Two_Numbers:2021-LeetCode-02_Add_Two

    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

    AddTwoNumbers

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

    add-two-numbers

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

    Two Sum leetcode c++

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

    程序员面试宝典LeetCode刷题手册

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

    leetcode下载-playground:玩儿

    Daily-LeetCode02AddTwoNumbers- 5.spark包 详情参见 6.unsafe包 闲暇时看一些Java关于unsafe的文章时写的一些代码 7.algorithm包-算法 自己的一些白话理解 1.选择排序,时间复杂度是O(n^2),空间复杂度是O(1),不稳定...

    LeetCode最全代码

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

    leetcode2sumc-add-two-numbers-solution:我的LeetCodeC解决方案:添加两个数字

    Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output...

    leetcode分类-LeetCodeSrc:力扣

    O(n)时间复杂度就可以解决###LeetCode4AddTwoNumbers简单题,注意空指针的情况和进位###LeetCode5LongestPalindromicSubstringO(N^2)的时间复杂度,不知道有没有更快的###LeetCode6ZigZagConversion边界条件一定要想...

    leetcode卡-LeetCode:我的LeetCode解决方案

    Add Two Numbers], Linked list 2017.06.13 打卡[LeetCode 200. Number of Islands], BFS 2017.06.14 打卡[LeetCode 3. Longest Substring Without Repeating Characters], N/A 2017.06.15 打卡[LeetCode 407. ...

    lrucacheleetcode-leetcode:leetcode

    Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 5. Longest Palindromic Substring 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water ...

    leetcode答案-leetcode:javascript中leetcode测试的答案

    leetcode 答案leetcode javascript 中 leetcode 测试的答案 twoSum addTwoNumbers

    leetcode所有报错-leetcode:leetcode

    Add Two Numbers struct复习 可以在定义结构体的同时定义结构体变量: struct stu{ char *name; //姓名 int num; //学号 int age; //年龄 char group; //所在学习小组 float score; //成绩 } stu1, stu2; 如果只需要...

Global site tag (gtag.js) - Google Analytics