Question 4 of leetcode, add two numbers

Source: Internet
Author: User

Original question:

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

Question Analysis:

Two linked lists are given to indicate two non-negative values. Numbers are stored in reverse order. Each node contains a single number. Calculate the sum of the two linked lists and return the results in the same format.

The solution is to directly add the linked list ..

The Code is as follows:

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;} elselast = 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


One piece of water ....



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.