1 題目
You are giventwo linked lists representing two non-negative numbers. The digits are storedin reverse order
and each of their nodes contain a single digit. Add the twonumbers and return it as a linked list.
Input: (2 -> 4 -> 3)+ (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
2 分析
該題目屬於鏈表的相加,須要注意的有:
(1) 考慮兩個鏈表的長度,尤爲是鏈表爲空時也能處理。
(2) 每一個結點只表示一位數字。
(3) 當鏈表末尾結點相加後如有進位,則須要申請新的結點存儲信息。node
3 核心代碼:ios
int listLength(ListNode* head) //遞歸獲得鏈表的長度 { return head ? 1 + listLength(head ->next) : 0; } ListNode *addTwoNumbers(ListNode *l1, ListNode *l2){ //把兩個鏈表相加(前面的參數爲長鏈表) if (listLength(l1) < listLength(l2)){ return addTwoNumbers(l2, l1); } ListNode *head1 = l1, *head2 = l2; int inc = 0; bool isEnd = false; while (head2){ int val = head1 -> val + head2-> val + inc; head1 -> val = val % 10; inc = val / 10; if (head1 -> next){ head1 = head1 -> next; }else{ isEnd = true; } head2 = head2 -> next; } while (inc){ //當短鏈表計算完以後 int val = isEnd ? inc : head1 ->val + inc; if (isEnd){ head1 -> next = new ListNode(val % 10); }else{ head1 -> val = val % 10; } inc = val / 10; if (head1 -> next){ head1 = head1 -> next; }else{ isEnd = true; } } return l1; }
4 如下爲代碼的完整實現git
#include "stdafx.h" #include<string.h> #include<iostream> using namespace std; struct ListNode { //sizeof(ListNode):8 int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; int listLength(ListNode* head) //遞歸獲得鏈表的長度 { return head ? 1 + listLength(head ->next) : 0; } ListNode *addTwoNumbers(ListNode *l1, ListNode *l2){ //把兩個鏈表相加(前面的參數爲長鏈表) if (listLength(l1) < listLength(l2)){ return addTwoNumbers(l2, l1); } ListNode *head1 = l1, *head2 = l2; int inc = 0; bool isEnd = false; while (head2){ int val = head1 -> val + head2-> val + inc; head1 -> val = val % 10; inc = val / 10; if (head1 -> next){ head1 = head1 -> next; }else{ isEnd = true; } head2 = head2 -> next; } while (inc){ //當短鏈表計算完以後 int val = isEnd ? inc : head1 ->val + inc; if (isEnd){ head1 -> next = new ListNode(val % 10); }else{ head1 -> val = val % 10; } inc = val / 10; if (head1 -> next){ head1 = head1 -> next; }else{ isEnd = true; } } return l1; } //****************************************************************** ListNode *createList(int arr[],int len){ //根據數組建立鏈表 ListNode* head = NULL,*p; p=head; for(int i=0; i<len; i++){ if(i==0){ head = new ListNode(arr[i]); p = head; }else{ p->next = new ListNode(arr[i]); p = p->next; } } return head; } void printList(ListNode *head){ //輸出鏈表 ListNode *p = head; if(head==NULL) cout<<"空鏈表"<<endl; else{ while(p){ cout<<p->val<<" "; p = p->next; } cout<<endl; } } int main(int argc, char * argv[]) { //32位系統中全部指針(包括複雜結構體)均爲4個字節,float、long和int類型爲4個字節,char爲1個字節,double爲8個字節 //char arr[] = "12345678"; //strlen:8 sizeof:9 sizeof(arr)/sizeof(arr[0]):9 //做爲參數時 strlen:8 sizeof:4 sizeof(arr)/sizeof(arr[0]):4 //int arr[] = {1,2,3,4,5,6,7,8}; //strlen:x sizeof:32 sizeof(arr)/sizeof(arr[0]):8 //做爲參數時 strlen:x sizeof:4 sizeof(arr)/sizeof(arr[0]):1 int arr1[] = {2, 4, 3}; //表示從低位到高位 int arr2[] = {5, 6, 4}; int len1 = sizeof(arr1)/sizeof(arr1[0]); int len2 = sizeof(arr2)/sizeof(arr2[0]); ListNode *l1 = createList(arr1,len1); ListNode *l2 = createList(arr2,len2); printList(addTwoNumbers(l1,l2)); system("pause"); return 0; }