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 -> 4)
Output: 7 -> 0 -> 8

题意解析:

给定两个链表表示两个非负数。数字逆序存储,每个节点包含一个单一的数字。计算两个链表表示的数的和,并以同样格式的链表的形式返还结果。

解法就是直接操作链表相加就可以了。。

代码如下:

C++

class Solution {
public:
	ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
		ListNode * ans = NULL, *last = NULL;
		int up = 0;
		while (NULL != l1 && NULL != l2) {
			int tmp = l1->val + l2->val + up;
			up = tmp / 10;
			if (NULL == last) {
				ans = new ListNode(tmp % 10);
				last = ans;
			} else
			last = pushBack(last, tmp % 10);
			l1 = l1->next;
			l2 = l2->next;
		}
		while (NULL != l1) {
			int tmp = l1->val + up;
			last = pushBack(last, tmp % 10);
			up = tmp / 10;
			l1 = l1->next;
		}
		while (NULL != l2) {
			int tmp = l2->val + up;
			last = pushBack(last, tmp % 10);
			up = tmp / 10;
			l2 = l2->next;
		}
		if (0 != up) {
			ListNode * l = new ListNode(up);
			last->next = l;
		}
		return ans;
	}

	ListNode * pushBack(ListNode * last, int val) {
		ListNode * l = new ListNode(val);
		last->next = l;
		return l;
	}
};

Python

# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    # @return a ListNode
    def addTwoNumbers(self, l1, l2):
        carry = 0; head = ListNode(0); curr = head;
        while l1 and l2:
            Sum = l1.val + l2.val + carry
            carry = Sum / 10
            curr.next = ListNode(Sum % 10)
            l1 = l1.next; l2 = l2.next; curr = curr.next
        while l1:
            Sum = l1.val + carry
            carry = Sum / 10
            curr.next = ListNode(Sum % 10)
            l1 = l1.next; curr = curr.next
        while l2:
            Sum = l2.val + carry
            carry = Sum / 10
            curr.next = ListNode(Sum % 10)
            l2 = l2.next; curr = curr.next
        if carry > 0:
            curr.next = ListNode(carry)
        return head.next

水一枚。。。。

LeetCode第四题,Add Two Numbers

时间: 2024-10-13 15:31:46

LeetCode第四题,Add Two Numbers的相关文章

Leetcode 线性表 数 Add Two Numbers

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Add Two Numbers Total Accepted: 13127 Total Submissions: 58280 You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes

LeetCode 第 67 题 (Add Binary)

LeetCode 第 67 题 (Add Binary) Given two binary strings, return their sum (also a binary string). For example, a = "11" b = "1" Return "100". 两个字符串,计算加法.这道题主要是考察对字符串操作的掌握情况.另外,加法要从低位算起,但是输出时要先输出高位.因此,需要将计算结果先存下来,然后再逆序输出. 访问字符串的

leetcode_2_题——Add Two Numbers(链表)

Add Two Numbers Total Accepted: 58510 Total Submissions: 268082My Submissions Question Solution 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 digi

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: (2 -> 4 -> 3) + (5 -> 6 -&

[LeetCode In C++] 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: (2 -> 4 -> 3) + (5 -> 6 -&

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: (2 -> 4 -> 3) + (5 -> 6 ->

第13题 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

好吧,其实我的代码很冗杂 噗 其实也就是链表的基础知识. /** * Definition for singly-linked list. * function ListNode(val) { * this.val = val; * this.next = null; * } */ /** * @param {ListNode} l1 * @param {ListNode} l2 * @return {ListNode} */ var addTwoNumbers = function(l1, l

LeetCode第四题Median of Two Sorted Arrays解法

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). example: nums1 = [1, 3] nums2 = [2] The median is 2.0 简单来说,这道题的要求就是求两个数组的中位数,因