[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;
}
};


2. Add Two Numbers leetcode(list)

Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits a...

leetcode题解-82. Remove Duplicates from Sorted List II && 445. Add Two Numbers II

82, 题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distin...

[CrackCode] 2.4 Add two numbers and return the sum as a linked list

You have two numbers represented by a linked list, where each node contains a single digit The digit...

和大神们学习每天一题(leetcode)-Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...

LeetCode 445. Add Two Numbers II--两个链表均按照由尾部到头部计算两个结点数值之和,保持进位

You are given two non-empty linked lists representing two non-negative integers. The most significan...

Leetcode: Add Two Numbers (2)

You are given two linked lists representing two non-negative numbers. The digits are stored in orde...
  • lzmaths
  • lzmaths
  • 2013年12月18日 14:19
  • 399

[leetcode NO.3] Add Two Numbers (JAVA)

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...

LeetCode:Add Two Numbers

这是第一次用markdown写东西啊,本来写的就少,试一试吧。 题目: You are given two linked lists representing two non-negative...

【Leetcode】445. Add Two Numbers II

445. Add Two Numbers II You are given two non-empty linked lists representing two non-negative ...

LeetCode Algorithms 2. Add Two Numbers 题解

题目: You are given two non-empty linked lists representing two non-negative integers. The digits...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode][list] Add Two Numbers
举报原因:
原因补充:

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