Add Two Numbers

来源:互联网 发布:matlab提取矩阵某一列 编辑:程序博客网 时间:2024/06/03 10:35

题目来源:

https://leetcode.com/problems/add-two-numbers/description/

题目描述:

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

心路历程:

这道题在leetcode上的难度标示为Medium,但感觉不是太难,主要考查结构体和链表的运用。其中有些特殊情况,是在提交之后报错才知道的。比如,要考虑最高位的进位。还有一个关键点,就是两个链表长度不一致时,要把比较短的那个链表高位用0来代替。

解决方案:

1.自己写的C++版解法,遇到的一个坑是,结构体等于NULL的时候,不能再指向下一个节点。(一开始天真地以为NULL的下一个节点还是NULL)
#include<iostream>using namespace std;struct ListNode {    int val;    ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};class Solution {public:ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {ListNode* head = new ListNode(0);ListNode* result = head;int flag = 0;int add1, add2, sum, rem;//加数1,加数2,和,余数int carry = 0;//进位while (l1 != NULL || l2 != NULL) {//只要任意一个链表不为空则继续if (l1 == NULL) add1 = 0; else add1 = l1->val;//空的链表赋值为0if (l2 == NULL) add2 = 0; else add2 = l2->val;sum = add1 + add2 + carry;carry = sum / 10;rem = sum % 10;if (flag == 0) { //链表头直接赋值head->val = rem;flag++;}else { //新建节点head->next = new ListNode(rem);head = head->next;}if (l1 != NULL)l1 = l1->next;if (l2 != NULL) l2 = l2->next;}if (carry == 0){}//处理最高位else {head->next = new ListNode(carry);head = head->next;}return result;}};int main() {ListNode* l1 = new ListNode(0);ListNode* l2 = new ListNode(0);ListNode* arg1 = l1;ListNode* arg2 = l2;l1->val = 2;l1->next = new ListNode(4);l1 = l1->next;l1->next = new ListNode(3);l2->val = 5;l2->next = new ListNode(6);l2 = l2->next;l2->next = new ListNode(4);Solution solution;ListNode* result = solution.addTwoNumbers(arg1, arg2);while (result != NULL) {cout << result->val;result = result->next;}cout << endl;system("pause");}
2.第二种解决方案来自leetcode,用Java写的,思路与第一种基本一致。主要的优化是采用了虚拟链表头,使得循环时不需要进行一次判断,最后返回链表头的下一个节点即可。这种思路值得借鉴。代码如下:
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {    ListNode dummyHead = new ListNode(0);    ListNode p = l1, q = l2, curr = dummyHead;    int carry = 0;    while (p != null || q != null) {        int x = (p != null) ? p.val : 0;        int y = (q != null) ? q.val : 0;        int sum = carry + x + y;        carry = sum / 10;        curr.next = new ListNode(sum % 10);        curr = curr.next;        if (p != null) p = p.next;        if (q != null) q = q.next;    }    if (carry > 0) {        curr.next = new ListNode(carry);    }    return dummyHead.next;

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 qq红包的密码忘了怎么办 qq密码被改了怎么办啊 手机qq密码忘了怎么办 腾讯安全中心冻结解冻进不去怎么办 qq钱包充值限额怎么办 qq没绑卡支付密码忘了怎么办 手机qq停止运行该怎么办 手机不支持qq软件运行怎么办 王者传奇冲元宝不到账怎么办 支付宝充值地下城点卷冲错了怎么办 dnf点券冲错了怎么办 百家号改了手机绑定找不到了怎么办 银行卡换了网银怎么办 qq没有银行卡转不出钱怎么办 qq钱包限制一万怎么办 qq余额超过20万怎么办 扣扣忘记了密码怎么办 qq钱包发不出来怎么办 qq支付密码忘了怎么办? 扣扣上转账错了怎么办 qq绑卡存在异常怎么办 微信没绑银行卡忘记支付密码怎么办 微信的自动扣费怎么办 不小心把钱充到微信财付通该怎么办 财付通用什么充值卡充值话费怎么办 苹果手机灯坏了怎么办 手机电灯不亮了怎么办? 苹果手机相机坏了怎么办 苹果5s手机背光灯不亮怎么办 苹果5s灯控坏了怎么办 微信q币冲错号码怎么办 支付宝转账到之前号码怎么办 qq红包收不了钱怎么办 qq抢红包要实名认证怎么办 支付宝被骗冲q币怎么办 qq发红包发错了怎么办 qq红包发不出来怎么办 qq红包发多了怎么办 qq发红包要短信验证怎么办 不是qq好友发了红包怎么办 苹果手机升级后支付宝打不开怎么办