关闭

2. Add Two Numbers

标签: leetcode
115人阅读 评论(0) 收藏 举报
分类:

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) {
        ListNode res(0);
        ListNode *tail = &res;
        div_t sum = {0, 0};
        while(sum.quot > 0 || L1 || L2){
            if(L1){
                sum.quot += L1->val;
                L1 = L1->next;
            }
            if(L2){
                sum.quot += L2->val;
                L2 = L2->next;
            }
            sum = div(sum.quot, 10);
            tail->next = new ListNode(sum.rem);//注意 这里不是头结点res, 而是变量tail->
            tail = tail->next;
        }
        return res.next;//注意这里 不是 res->next;
    }
};


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【python】【leetcode】【算法题目2—Add Two Numbers】

【python】【leetcode】【算法题目2—Add Two Numbers】 题目原文: You are given two linked lists representing two non-negative numbers. The digits are stored in revers...
  • u014615155
  • u014615155
  • 2016-11-21 12:05
  • 1812

2. Add Two Numbers官方通过版本(8.46%)

public class AddTwoNumbers { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode sumNumbers = new ListNode(0); // 进位 ...
  • ithouse
  • ithouse
  • 2016-01-05 15:58
  • 236

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...
  • u011809767
  • u011809767
  • 2017-02-16 09:17
  • 488

第十五周(Add Two Numbers)

第十四周(subsets,NP问题) 目录: 本周完成题目 主要过程思路 相关代码 一、本周完成题目 本周共完成2道题目,2道Medium。主要是关于子集的两个问题。 具体完成题目及难度如下表: # Title Difficulty 78 ...
  • qq_24486635
  • qq_24486635
  • 2017-05-30 15:01
  • 94

算法练习:Add Two Numbers

https://leetcode.com/problems/add-two-numbers/?tab=Description
  • zhangf21
  • zhangf21
  • 2017-02-24 11:18
  • 153

freemarker写select组件报错总结(五)

1、错误描述 六月 26, 2014 10:44:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template processing error: "The only legal comparisons are...
  • you23hai45
  • you23hai45
  • 2014-06-26 22:47
  • 2726

Add Two Numbers(基于链表的两数相加)

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 ...
  • ajiangfan
  • ajiangfan
  • 2016-12-03 14:26
  • 317

(LeetCode)算法题目——Add Two Numbers

Add Two Numbers
  • lh470342237
  • lh470342237
  • 2017-07-31 22:47
  • 59

每日算法之四:Add Two Numbers

题目要求如下:
  • yapian8
  • yapian8
  • 2014-04-13 21:18
  • 572

第一天 Add Two Numbers(链表加法)

这是leetcode第二题,对链表的知识基础有一定要求。暂时自己写不出来,先完全解析别人的代码。 1. 先排除极端情况,简化后续 if(l1 == NULL && l2) return l2; if(l1 && l2 == NULL) return l1; ...
  • Taylor_ChaosDreamer
  • Taylor_ChaosDreamer
  • 2016-07-19 20:59
  • 205
    个人资料
    • 访问:13904次
    • 积分:1088
    • 等级:
    • 排名:千里之外
    • 原创:99篇
    • 转载:5篇
    • 译文:0篇
    • 评论:0条