LeetCode 2_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 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、考虑最后一个节点有可能进位,此时需要新建节点。
首先不加思索写出的代码:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* l3 = l1; int tmp1 = 0, tmp = 0; while(l1->next != NULL && l2->next != NULL) { tmp1 = l1->val; l1->val = (l1->val + l2->val + tmp) % 10; tmp = (tmp1 + l2->val + tmp) / 10; l1 = l1->next; l2 = l2->next; } if(l1->next == NULL && l2->next == NULL) { tmp1 = l1->val; l1->val = (l1->val + l2->val + tmp) % 10; tmp = (tmp1 + l2->val + tmp) / 10; if(tmp == 1) l1->next = new ListNode(1); return l3; } if(l1->next != NULL) { tmp1 = l1->val; l1->val = (l1->val + l2->val + tmp) % 10; tmp = (tmp1 + l2->val + tmp) / 10; l1 = l1->next; while(l1->next != NULL) { if(l1->val + tmp < 10) { l1->val = l1->val + tmp; return l3; } else { l1->val = (l1->val + tmp) % 10; tmp = 1; l1 = l1->next; } } if(l1->val + tmp >= 10) { l1->val = (l1->val + tmp) % 10; l1->next = new ListNode(1); return l3; } else { l1->val = (l1->val + tmp) % 10; return l3; } } if(l2->next != NULL) { tmp1 = l1->val; l1->val = (l1->val + l2->val + tmp) % 10; tmp = (tmp1 + l2->val + tmp) / 10; l1->next = l2->next; l1 = l1->next; while(l1->next != NULL) { if(l1->val + tmp < 10) { l1->val = l1->val + tmp; return l3; } else { l1->val = (l1->val + tmp) % 10; tmp = 1; l1 = l1->next; } } if(l1->val + tmp >= 10) { l1->val = (l1->val + tmp) % 10; l1->next = new ListNode(1); return l3; } else { l1->val = (l1->val + tmp) % 10; return l3; } } }
这代码,看起来好low,首先代码重复性太大,从代码的编写就可以有大量的优化空间。
最终优化为(其实也没什么优化,只是删掉了一些重复):
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { ListNode* l3 = l1; int tmp1 = 0, tmp = 0; while(l1->next != NULL && l2->next != NULL) { tmp1 = l1->val; l1->val = (l1->val + l2->val + tmp) % 10; tmp = (tmp1 + l2->val + tmp) / 10; l1 = l1->next; l2 = l2->next; } tmp1 = l1->val; l1->val = (l1->val + l2->val + tmp) % 10; tmp = (tmp1 + l2->val + tmp) / 10; if(l1->next == NULL && l2->next == NULL) { if(tmp == 1) l1->next = new ListNode(1); return l3; } else { if(l2->next != NULL) l1->next = l2->next; l1 = l1->next; while(l1->next != NULL) { if(l1->val + tmp < 10) { l1->val = l1->val + tmp; return l3; } else { l1->val = (l1->val + tmp) % 10; tmp = 1; l1 = l1->next; } } if(l1->val + tmp >= 10) { l1->val = (l1->val + tmp) % 10; l1->next = new ListNode(1); return l3; } else { l1->val = (l1->val + tmp) % 10; return l3; } }</span>
其实有一种更简单的方法是再新建一个链表,不过就是会浪费空间
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { int addNum = 0; ListNode *l3 = new ListNode(0); ListNode *ptr = l3; while(l1 != NULL || l2 != NULL) { int val1 = 0; if(l1 != NULL) { val1 = l1->val; l1 = l1->next; } int val2 = 0; if(l2 != NULL) { val2 = l2->val; l2 = l2->next; } ListNode *temp = new ListNode((val1 + val2 + addNum) % 10); ptr->next = temp; ptr = temp; addNum = (val1 + val2 + addNum) / 10; } if(addNum == 1) ptr->next = new ListNode(1); return l3->next; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-18 10:58:58