Python (I haven't been able to stand my answer short enough qaq):
"" "Programmer:eofdate:2015.04.14file:atn.pye-mail: [email protected]" "" # Definitio N for singly-linked list.class listnode:def __init__ (self, x): Self.val = x self.next = Noneclass Soluti On: # @return a ListNode def addtwonumbers (self, L1, L2): ret_list = ListNode (0) iter_list = ret_list remainder = 0 quotient = 0 while L1! = None and L2! = None:rema Inder = (l1.val + l2.val + iter_list.val)% quotient = (L1.val + l2.val + iter_list.val)/10 Iter_list.val = Remainder Iter_list.next = listnode (quotient) Last_prev = Iter_list Iter_list = iter_list.next L1 = L1.next L2 = L2.next if L1! = None: While L1 are not None:remainder = (l1.val + iter_list.val)% quotient = (L1.val + iter_list.val)/iter_list.val = remainder Iter_list.next = ListNode (quotient) Last _prev = iter_list Iter_list = iter_list.next L1 = L1.next if L2! = None: While L2 are not None:remainder = (l2.val + iter_list.val)% q Uotient = (l2.val + iter_list.val)/iter_list.val = remainder Iter_list.next = Listnod E (quotient) Last_prev = iter_list Iter_list = Iter_list.next L2 = L2.next If Iter_list is not none and iter_list.val = = 0:del Last_prev.next Last_prev.next = None Return ret_list def create_list (self, num): iterator = ListNode (num[0]) Ret_val = iterator i = 1 While I < Len (num): Iterator.next = ListNode (num[i]) iterator = Iterator.next i + = 1 return ret_val#---------just for testing------s = solution () print s.addtwonumbers (S.create_list ([0]), s.create_list ([0])) print S.addtwonumbers (S.create_list ([3,6]), s.create_list ([8,5]))
The Java implementation of the triumph charge:
Package Add_two_numbers;class ListNode {int val; ListNode Next; ListNode (int x) {val = X;next = null;}} public class Solution {public ListNode addtwonumbers (listnode L1, ListNode L2) {ListNode pseudohead = new ListNode (0); ListNode tail = Pseudohead;int carry = 0;while (L1! = NULL | | L2! = NULL | | Carry > 0) {if (L1! = null) {carry + = L1.va L;L1 = L1.next;} if (L2! = null) {carry + = L2.VAL;L2 = L2.next;} Tail.next = new ListNode (carry%); tail = Tail.next;carry/= 10;} return pseudohead.next;}}
The C + + version of the Hao God:
source:https://oj.leetcode.com/problems/add-two-numbers///Author:hao chen//date:2014-06-18/****************** * Given, linked lists representing, non- Negative numbers. * The digits is stored in reverse order and all of their nodes contain a single digit. * Add the numbers and return it as a linked list.* * Input: (2, 4, 3) + (5, 6, 4) * Output:7- 0-8* **********************************************************************************//** * Definit Ion 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) {int x=0, y=0, carry=0, sum=0 ; ListNode *h=null, **t=&h; while (L1!=null | | l2!=null) {x = Getvalueandmovenext (L1); y = GetvalueaNdmovenext (L2); sum = carry + x + y; ListNode *node = new ListNode (SUM%10); *t = node; t = (&node->next); carry = SUM/10; } if (Carry > 0) {listnode *node = new ListNode (CARRY%10); *t = node; } return h; }private:int Getvalueandmovenext (listnode* &l) {int x = 0; if (l! = NULL) {x = l->val; L = l->next; } return x; }};
Leetcode # ADD Numbers #