LeetCode # Add Two Numbers #





Python(我一直不能忍受我的解答不够简短QAQ):


"""
Programmer  :   EOF
Date        :   2015.04.14
File        :   atn.py
E-mail      :   jasonleaster@gmail.com

"""
# Definition for singly-linked list.

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

class Solution:
    # @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:
            remainder = (l1.val + l2.val + iter_list.val) % 10
            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 is not None:
                
                remainder = (l1.val + iter_list.val) % 10
                quotient  = (l1.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
            
        if l2 != None:
            while l2 is not None:
                
                remainder = (l2.val + iter_list.val) % 10
                quotient  = (l2.val + iter_list.val) / 10

                iter_list.val  = remainder
                iter_list.next = ListNode(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]) )


凯旋冲锋的Java实现:


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.val;
				l1 = l1.next;
			}
			if (l2 != null) {
				carry += l2.val;
				l2 = l2.next;
			}
			tail.next = new ListNode(carry % 10);
			tail = tail.next;
			carry /= 10;
		}
		return pseudoHead.next;
	}
}



皓神的C++版本:

// Source : https://oj.leetcode.com/problems/add-two-numbers/
// Author : Hao Chen
// Date   : 2014-06-18

/********************************************************************************** 
* 
* 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
*               
**********************************************************************************/

/**
 * 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) {
        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;
    }
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值