Leetcode# 2. Add Two Numbers(链表模拟大数算法)

Add Two Numbers(链表)

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

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

题意

给定两个非空的链表,表示两个非负整数。数字以相反的顺序存储,每一个节点包含一个数字。将两个数相加并将结果作为链表返回。
注意返回的结果的列表也是相反的。
运算过程:

2 4 3
5 6 4
—————
7 0 8

特别像字符串求大数算法,这里题目已经把字符串翻转,简单了不少。

解决方法一:C++版

思路很简单,麻烦之处在于指针的使用。sum指向头结点,,temp代表移动的指针,在末尾添加元素的操作为:
temp ->next = 当前数
指针后移:
temp ->next = next

/**
 * 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 carry = 0;//进位
        ListNode *sum = NULL, *temp = NULL;

        while(l1 != NULL || l2 != NULL || carry != 0)
        {
            int now = 0;//当前位数
            if(l1!=NULL)
            {
                now += l1 -> val;
                l1 = l1 -> next;
            }
            if(l2!=NULL)
            {
                now += l2 -> val;
                l2 = l2 -> next;
            }
            if(carry != 0)
                now += carry;

            carry = now / 10; 
            now = now % 10;
            //将int型的now转换为ListNode注意看上面的构造函数要传参数的
            ListNode *p = new ListNode(now);

            //头指针指向temp
            if(sum == NULL)
            {
                temp = p;
                sum  = temp;
            }
            else 
            {
                temp -> next = p;
                temp = temp -> next;
            }
        }
        return sum;
    }
};

解决方法二:Python版

解法思路同上。

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def addTwoNumbers(self, l1, l2):
        """
        :type l1: ListNode
        :type l2: ListNode
        :rtype: ListNode
        """
        sum = None
        temp = None
        carry = 0
        while l1 != None or l2 != None or carry != 0:
            now = 0
            if l1 != None:
                now = now + l1.val
                l1 = l1.next
            if l2 != None:
                now = now + l2.val
                l2 = l2.next
            if carry != 0:
                now = now + carry
            carry = now / 10
            now = now % 10
            q = ListNode(now)
            if sum == None:
                temp = q
                sum = temp
            else:
                temp.next = q
                temp = temp.next

        return sum

总结

这两种语言思路是一样的,如果不太懂大数算法的可以在这里学习一下:http://blog.csdn.net/xunalove/article/details/54599639

本地github代码:https://github.com/xuna123/LeetCode/blob/master/Leetcode%23%202.%20Add%20Two%20Numbers.md

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值