[leetcode][list] Add Two Numbers

原创 2015年07月07日 17:01:27

题目:

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

36ms

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
	if (l2 == NULL) return l1;
	if (l1 == NULL) return l2;
	int digit = (l1->val + l2->val) % 10;
	int carry = (l1->val + l2->val) / 10;
	ListNode *head = new ListNode(digit);
	ListNode *tail = head;
	ListNode *p1 = l1->next, *p2 = l2->next;
	while (p1 != NULL && p2 != NULL){
		digit = (p1->val + p2->val + carry) % 10;
		carry = (p1->val + p2->val + carry) / 10;
		ListNode *p = new ListNode(digit);
		tail->next = p;
		tail = p;
		p1 = p1->next;
		p2 = p2->next;
	}
	ListNode *pp = p1;
	if (p2 != NULL) pp = p2;
	while (pp != NULL){
		digit = (pp->val + carry) % 10;
		carry = (pp->val + carry) / 10;
		ListNode *p = new ListNode(digit);
		tail->next = p;
		tail = p;
		pp = pp->next;
	}
	if (carry != 0){
		ListNode *p = new ListNode(carry);
		tail->next = p;
	}
	return head;
}
};


LeetCode | Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in...
  • lanxu_yy
  • lanxu_yy
  • 2013年09月14日 17:06
  • 15466

2、add two numbers(python)

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...
  • qq_28119401
  • qq_28119401
  • 2016年09月18日 21:49
  • 484

LeetCode Add Two Numbers 之JavaScript 多种解法

LeetCode Add Two Numbers 之JavaScript 多种解法
  • weixin_36751895
  • weixin_36751895
  • 2017年07月13日 12:45
  • 386

【python】【leetcode】【算法题目2—Add Two Numbers】

一、题目描述 题目原文:  You are given two linked lists representing two non-negative numbers. ...
  • u014615155
  • u014615155
  • 2016年11月21日 12:05
  • 1949

【LeetCode】2. Add Two Numbers 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51820937Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016年10月24日 19:46
  • 2706

leetcode 2. Add Two Numbers C语言实现

leetcode 2. Add Two Numbers Description You are given two non-empty linked lists represent...
  • banana1006034246
  • banana1006034246
  • 2017年03月31日 19:48
  • 893

[LeetCode] 002. Add Two Numbers (Medium) (C++/Java/Python)

002.Add_Two_Numbers (Medium)
  • hcbbt
  • hcbbt
  • 2015年02月27日 12:43
  • 5635

[Leetcode] Add Two Numbers Python实现 详细讲解

本题是使得两个链表相加,每个链表中值均为0~9,对于两个链表对应的值相加值sum若大于9,则为sum%10,并在指向的下一对节点的和sum上加1。 做题思路: 判断两链表是否有空链表,若有,直接返回...
  • weixin_37510517
  • weixin_37510517
  • 2017年02月12日 16:54
  • 597

[LeetCode][2]Add Two Numbers解析 -Java实现

Q: You are given two linked lists representing two non-negative numbers. The digits are stored in re...
  • u014629433
  • u014629433
  • 2016年06月01日 16:34
  • 1930

【LeetCode with Python】 Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...
  • nerv3x3
  • nerv3x3
  • 2014年06月27日 18:11
  • 1077
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode][list] Add Two Numbers
举报原因:
原因补充:

(最多只允许输入30个字)