博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode: Adding two numbers (by list)
阅读量:6676 次
发布时间:2019-06-25

本文共 4572 字,大约阅读时间需要 15 分钟。

收获:1、程序调试一定要根据结果推理,不能乱试,至少要有根据的试

        2、单向链表遍历的时候要注意: 不能找到之前的元素。这样,在遍历到list末尾的时候,就不能给list添加元素了。

Problem: 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

思路:链表对应的位置相加即可。

难点:1、加法的进位处理;

        2、两个链表的遍历过程中在末尾如何处理。

代码:

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9 ListNode* initList();10 void listAdd(ListNode* last, int val);11 class Solution {12 public:13     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {14         // IMPORTANT: Please reset any member data you declared, as15         // the same Solution instance will be reused for each test case.16         ListNode *L1 = l1;17         ListNode *L2 = l2;18         ListNode *temp = NULL; // to keep the L1 finding the list's end 19         int increase = 0;20         while(L1 || L2 || increase){21             if(L1 && L2){ // L1 and L2 are not empty22                 int sum_val = L1->val + L2->val + increase;23                 L1->val = sum_val % 10;24                 increase = sum_val / 10;25                 temp = L1; // in case of L1 is the end of list26                 L1 = L1->next;27                 L2 = L2->next;28             }else if((NULL == L1) && (NULL != L2)){ // L1 empty29                 L1 = new ListNode(0);30                 temp->next = L1;31                 int sum_val = L2->val + increase;32                 L1->val = sum_val % 10;33                 increase = sum_val/10;34                 temp = L1;35                 L1 = L1->next;36                 L2 = L2->next;37             }38             else if((NULL != L1) && (NULL == L2)){39                 int sum_val = L1->val + increase;40                 L1->val = sum_val % 10;41                 increase = sum_val /10;42                 temp = L1;43                 L1 = L1->next;44             }45             else if(increase){46                 L1 = new ListNode(1);47                 temp->next = L1;48                 //L1 = temp;49                 //L1->val = 1;50                 increase = 0;51                 L1 = L1->next;52             }53         }54         return l1;55     }56 };

 

这个程序破坏了输入链表。这不好(根据Code Complete2)。下面的程序应该也可以。

1 /** 2  * Definition for singly-linked list. 3  * struct ListNode { 4  *     int val; 5  *     ListNode *next; 6  *     ListNode(int x) : val(x), next(NULL) {} 7  * }; 8  */ 9 ListNode* initList();10 void listAdd(ListNode* last, int val);11 class Solution {12 public:13     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {14         // IMPORTANT: Please reset any member data you declared, as15         // the same Solution instance will be reused for each test case.16         if((!l1) && (!l2)) return NULL;17         ListNode *L1 = l1;18         ListNode *L2 = l2;19         ListNode *res = new ListNode(0);20         ListNode *last = res;21         ListNode *temp = res;22         int flag = 0;23         24         while(L1 || L2 || flag){25             if(L1 && L2){ // L1 and L2 are not empty26                 int sum_val = L1->val + L2->val + flag;27                 if(NULL == last){ 28                     last = new ListNode(sum_val % 10);29                     temp->next = last;30                     temp = last;31                 }else{32                     last->val = (sum_val % 10); //33                 }34                 flag = sum_val/10;35                 last = last->next;36                 L1 = L1->next;37                 L2 = L2->next;38             }else if((NULL == L1) && (NULL != L2)){39                 int sum_val = L2->val + flag;40                 if(NULL == last){41                     last = new ListNode(sum_val % 10);42                     temp->next = last;43                     temp = last;44                 }else{45                     last->val = (sum_val % 10);46                 }47                 flag = sum_val/10;48                 last = last->next;49                 L2 = L2->next;50             }51             else if((NULL != L1) && (NULL == L2)){52                 L2 = L1;53                 L1 = NULL;54             }55             else if(flag){56                 last = new ListNode(1);57                 temp->next = last;58                 flag = 0;59             }60         }61         return res;   62     }63 };

 

转载于:https://www.cnblogs.com/jiangyoumiemie/p/3427126.html

你可能感兴趣的文章
CentOS 7 安装 Nginx
查看>>
程序员毒鸡汤:我们都该学会正确的失败
查看>>
在 JavaScript 中优雅的提取循环内的数据
查看>>
HTML-语义类标签
查看>>
cookie、session、cache-control等
查看>>
一篇文章带你理解闭包
查看>>
Android权限列表
查看>>
Sass基础
查看>>
Webpack3简单入门2
查看>>
Springmvc+mybatis+restful+bootstrap框架整合
查看>>
ubuntu下rsync服务器端和客户端的配置
查看>>
UNIX/Linux 系统管理技术手册阅读(八)
查看>>
LINUX 软件安装。
查看>>
linux 批量文件重命名
查看>>
javascript使用xml 数据岛 简单实例
查看>>
Ubuntu 16.04安装Cobbler 2.9
查看>>
源码编译安装mysql-5.7.14
查看>>
CentOS6.7 DNS配置
查看>>
vRealize Operations Manager 6.5的安装与配置
查看>>
老舍|回忆初恋,只一眼就一生
查看>>