Chinaunix首页 | 论坛 | 博客
  • 博客访问: 148202
  • 博文数量: 56
  • 博客积分: 245
  • 博客等级: 二等列兵
  • 技术积分: 520
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-08 14:43
个人简介

慢慢来

文章分类

全部博文(56)

文章存档

2017年(5)

2016年(2)

2015年(6)

2014年(28)

2013年(5)

2012年(10)

我的朋友

分类: C/C++

2014-08-20 23:01:56

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. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <string>

  5. using namespace std;

  6.  struct ListNode {
  7.      int val;
  8.      ListNode *next;
  9.      ListNode(int x) : val(x), next(NULL) {}
  10.  };

  11. class Solution {
  12. public:
  13.     ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
  14.         ListNode* current_result = new ListNode(0);
  15.         current_result->next = l1;
  16.         ListNode* result = l1;
  17.         int val;
  18.         
  19.         int over_ten_flag = 0;
  20.         
  21.         while(l1){
  22.             if(l2){
  23.                 val = (over_ten_flag + l1->val + l2->val) % 10;
  24.                 over_ten_flag = (over_ten_flag + l1->val + l2->val) / 10;
  25.                 l2 = l2->next;
  26.             } else{
  27.                 break;
  28.             }
  29.             current_result->next->val = val;
  30.             current_result = current_result->next;
  31.             l1 = l1->next;
  32.         }
  33.         
  34.         while(l1){
  35.             if ( l1->val != 9 || over_ten_flag == 0 ){
  36.                 current_result->next->val = current_result->next->val + over_ten_flag;
  37.                 over_ten_flag = 0;
  38.                 break;
  39.             }
  40.             current_result->next->val = 0;
  41.             current_result = current_result->next;
  42.             l1 = l1->next;
  43.         }
  44.         
  45.         if(l2) {
  46.             current_result->next = l2;
  47.         
  48.      while(l2){
  49.                 if ( l2->val != 9 || over_ten_flag == 0 ){
  50.                     current_result->next->val = current_result->next->val + over_ten_flag;
  51.                     over_ten_flag = 0;
  52.                     break;
  53.                 }
  54.      current_result->next->val = 0;
  55.      current_result = current_result->next;
  56.      l2 = l2->next;
  57.      }
  58.         }
  59.         
  60.         if(over_ten_flag){
  61.             ListNode* temp_node = new ListNode(1);
  62.             current_result->next = temp_node;
  63.         }
  64.         
  65.         return result;
  66.     }
  67. };

  68. int main(){
  69.     Solution* a = new Solution();
  70.     ListNode* n1 = new ListNode(5);
  71.     ListNode* n2 = new ListNode(4);
  72.     ListNode* n3 = new ListNode(3);
  73.     ListNode* n4 = new ListNode(5);
  74.     ListNode* n5 = new ListNode(6);
  75.     ListNode* n6 = new ListNode(4);

  76.     n1->next = n2;
  77. //    n2->next = n3;
  78.     
  79. //    n4->next = n5;
  80. //    n5->next = n6;
  81.     
  82.     ListNode* result = a->addTwoNumbers(n1, n4);
  83.     
  84.     cout << result->val << result->next->val << endl;
  85.     
  86.     bool negtive = 3>0;
  87.     cout << negtive;
  88.     
  89.     system("pause");
  90.     return 0;
  91. }

阅读(568) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~