2014-03-18 02:32

题目:给定两个由单链表表示的数字,返回它们的和。比如(9->9) + (1->2) = 0->2->1,99 + 21 = 120。

解法:逐位相加,注意处理进位、长度不等。

代码:

  1 // 2.5 Given two numbers represented by two lists, write a function that returns sum list. The sum list is list representation of addition of two input numbers.
  2 // Example First List: 5->6->3 // represents number 365 
  3 // Second List: 8->4->2 // represents number 248 
  4 // Resultant list: 3->1->6 // 
  5 // Note :Any Carry forward should also be added as the new node . Any Comments on the code below
  6 #include <cstdio>
  7 using namespace std;
  8 
  9 struct ListNode {
 10     int val;
 11     ListNode *next;
 12     ListNode(int x): val(x), next(nullptr) {};
 13 };
 14 
 15 class Solution {
 16 public:
 17     ListNode* listAddition(ListNode *head1, ListNode *head2) {
 18         if (head1 == nullptr) {
 19             return head2;
 20         } else if (head2 == nullptr) {
 21             return head1;
 22         }
 23         
 24         int carry = 0;
 25         int val;
 26         ListNode *p1, *p2;
 27         ListNode *head, *tail;
 28         
 29         p1 = head1;
 30         p2 = head2;
 31         head = tail = nullptr;
 32         while (p1 != nullptr && p2 != nullptr) {
 33             val = p1->val + p2->val + carry;
 34             carry = val / 10;
 35             val %= 10;
 36             if (head == nullptr) {
 37                 head = tail = new ListNode(val);
 38             } else {
 39                 tail->next = new ListNode(val);
 40                 tail = tail->next;
 41             }
 42             p1 = p1->next;
 43             p2 = p2->next;
 44         }
 45         while (p1 != nullptr) {
 46             val = p1->val + carry;
 47             carry = val / 10;
 48             val %= 10;
 49             tail->next = new ListNode(val);
 50             tail = tail->next;
 51             p1 = p1->next;
 52         }
 53         while (p2 != nullptr) {
 54             val = p2->val + carry;
 55             carry = val / 10;
 56             val %= 10;
 57             tail->next = new ListNode(val);
 58             tail = tail->next;
 59             p2 = p2->next;
 60         }
 61         if (carry) {
 62             tail->next = new ListNode(carry);
 63             tail = tail->next;
 64         }
 65         
 66         return head;
 67     }
 68 };
 69 
 70 int main()
 71 {
 72     int i;
 73     int n1, n2;
 74     int val;
 75     struct ListNode *head, *head1, *head2, *ptr;
 76     Solution sol;
 77     
 78     while (scanf("%d%d", &n1, &n2) == 2 && n1 > 0 && n2 > 0) {
 79         // create two linked lists
 80         ptr = head1 = nullptr;
 81         for (i = 0; i < n1; ++i) {
 82             scanf("%d", &val);
 83             if (head1 == nullptr) {
 84                 head1 = ptr = new ListNode(val);
 85             } else {
 86                 ptr->next = new ListNode(val);
 87                 ptr = ptr->next;
 88             }
 89         }
 90         ptr = head2 = nullptr;
 91         for (i = 0; i < n2; ++i) {
 92             scanf("%d", &val);
 93             if (head2 == nullptr) {
 94                 head2 = ptr = new ListNode(val);
 95             } else {
 96                 ptr->next = new ListNode(val);
 97                 ptr = ptr->next;
 98             }
 99         }
100         
101         // add up the two lists
102         head = sol.listAddition(head1, head2);
103         
104         // print the list
105         printf("%d", head->val);
106         ptr = head->next;
107         while (ptr != nullptr) {
108             printf("->%d", ptr->val);
109             ptr = ptr->next;
110         }
111         printf("\n");
112         
113         // delete the list
114         while (head != nullptr) {
115             ptr = head->next;
116             delete head;
117             head = ptr;
118         }
119         while (head1 != nullptr) {
120             ptr = head1->next;
121             delete head1;
122             head1 = ptr;
123         }
124         while (head2 != nullptr) {
125             ptr = head2->next;
126             delete head2;
127             head2 = ptr;
128         }
129     }
130     
131     return 0;
132 }

 

 posted on 2014-03-18 02:36  zhuli19901106  阅读(301)  评论(0编辑  收藏  举报