Leetcode 2-add two Numbers (c + + Java Python)

Source: Internet
Author: User

Title: http://oj.leetcode.com/problems/add-two-numbers/

You are given two linked lists representing two non-negative. The digits are stored in reverse order and all of their nodes a single contain. Add the two numbers and return it as a linked list.

Input: (2-> 4-> 3) + (5-> 6-> 4)
Output:7-> 0-> 8

Title translation:

A given two lists represent two non-negative numbers. The numbers are stored in reverse order, and each node contains a single number. Calculates the two-digit number and returns it in the form of a linked list. Analysis:

To consider rounding. The node that represents the result is to be new.

C + + implementation:

/** * 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) {//Important:please reset A

		NY member data you declared, AS//the same Solution instance'll be reused for each test case.
		if (L1 = = NULL) {return L2;
		} if (L2 = = NULL) {return L1;
		} ListNode *result = NULL;

		ListNode *sum = NULL;
		int val = 0;

		int carry = 0;
			while (L1!= null | | | L2!= NULL) {val = carry;
			if (L1!= NULL) {val + = l1->val;
			} if (L2!= NULL) {val + = l2->val;
			} carry = VAL/10;

			Val-= carry * 10;
				if (sum = = NULL) {sum = new ListNode (val);
			result = SUM;
				else {sum->next = new ListNode (val);
			sum = sum->next;
			} if (L1!= NULL) {L1 = l1->next; } if (L2!= NULL) {L2 = L2->next;
		} if (carry!= 0) {sum->next = new ListNode (carry);
    return result; }
};

Java implementation: (unlike C + + implementations)

/** * Definition for singly-linked list.
 * public class ListNode {* int val;
 * ListNode Next;
 * ListNode (int x) {* val = x;
 * next = NULL; *} */public class Solution {public ListNode addtwonumbers (listnode L1, ListNode L2) {//Importan
		T:please Reset any member of the data you declared, AS//the same Solution instance'll be reused to each test case.
		if (L1 = = null) {return L2;
		} if (L2 = = null) {return L1;
		int len1 = 0;

		int len2 = 0;

		ListNode head = L1;
			while (head!= null) {++len1;
		head = Head.next;

		head = L2;
			while (head!= null) {++len2;
		head = Head.next; } listnode longer = Len1 >= len2?
		L1:L2; ListNode shorter = Len1 < len2?

		L1:L2;
		ListNode result = null;

		ListNode sum = null;
		int val = 0;

		int carry = 0;
			while (shorter!= null) {val = longer.val + shorter.val + carry;
			carry = VAL/10;
			
			Val-= carry * 10; if (sum == null) {sum = new ListNode (val);
			result = SUM;
				else {sum.next = new ListNode (val);
			sum = Sum.next;
			} longer = Longer.next;
		shorter = Shorter.next;
			while (longer!= null) {val = longer.val + carry;
			carry = VAL/10;
			
			Val-= carry * 10;
			Sum.next = new ListNode (val);
			
			sum = Sum.next;
		longer = Longer.next;
		} if (carry!= 0) {sum.next = new ListNode (carry);
    return result; }
}

Python implementation:

# Definition for singly-linked list. # class ListNode: # def __init__ (self, x): # self.val = x # self.next = None class Solution: # @r
            Eturn a ListNode def addtwonumbers (self, L1, L2): If L1 = None:return L2 If L2 = None:
            return L1 len1 = 0 Len2 = 0 head = L1 while head!= None: Len1 + 1 head = Head.next head = L2 while head!= None:len2 +
        = 1 head = Head.next if len1 >= len2:longer = L1 shorter = L2
            Else:longer = L2; shorter = L1 sum = None carry = 0 while shorter!= none:va
            
            Lue = Longer.val + Shorter.val + carry carry = VALUE/10 value-= carry * 10
  if sum = = None:sum = ListNode (value)              result = Sum Else:sum.next = ListNode (value) sum = Sum.next longer = longer.next shorter = Shorter.next while longer!= Non
            
            E:value = Longer.val + carry carry = VALUE/10 value-= carry * 10
            
        Sum.next = ListNode (value) sum = sum.next longer = Longer.next If carry!= 0:sum.next = ListNode (carry) return result

        Thank you for reading and welcome comments.

Related Article

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.