leetcode_2

You are given two non-empty linked lists representing two non-negative integers. 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.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.

第一次的写法,过于复杂繁琐,考虑了太多因素,也导致了很多代码的重读

class Solution {
	public:
		ListNode* addTwoNumbers(ListNode* l1,ListNode* l2) {
			if (l1==NULL||l2==NULL) {
				return NULL;
			}
			ListNode* ptr1 = l1;
			ListNode* ptr2 = l2;
			ListNode* l3 = new ListNode(0);
			ListNode* ptr3 = l3;


			while(ptr1!=NULL && ptr2!=NULL) {
				ptr3->val=ptr1->val+ptr2->val;
				if(ptr3->val >= 10) {
					ptr3->val = ptr3->val%10;
					ptr3->next = new ListNode(1);
					ptr3 = ptr3->next;
					if(ptr1->next==NULL && ptr2->next==NULL) {
						ptr1 = ptr1->next;
						ptr2 = ptr2->next;
					}
					else if(ptr1->next==NULL && ptr2->next!=NULL) {
						ptr1->next=new ListNode(0);
						ptr1 = ptr1->next;  
						ptr2 = ptr2->next;
						ptr2->val+=1;
					}
					else if(ptr2->next==NULL && ptr1->next!=NULL) {
						ptr2->next=new ListNode(0);
						ptr2 = ptr2->next;
						ptr1 = ptr1->next;
						ptr1->val+=1;
					}
					else if (ptr1->next!=NULL && ptr2->next!=NULL) {
						ptr1 = ptr1->next;
						ptr1->val+=1;
						ptr2 = ptr2->next;
					}
				} else {
					ptr3->next = new ListNode(0);
					if(ptr1->next==NULL && ptr2->next==NULL) {
						ptr1 = ptr1->next;
						ptr2 = ptr2->next;
						delete ptr3->next;
						ptr3->next=NULL;
						break;
					}
					else if(ptr1->next==NULL && ptr2->next!=NULL) {
						ptr1->next=new ListNode(0);
						ptr1 = ptr1->next; 
						ptr2 = ptr2->next;
					}
						
					else if(ptr2->next==NULL && ptr1->next!=NULL) {
						ptr2->next=new ListNode(0);
						ptr2 = ptr2->next; 
						ptr1 = ptr1->next;
					}
						
					else if (ptr1->next!=NULL && ptr2->next!=NULL) {
						ptr1 = ptr1->next;
						ptr2 = ptr2->next;
					}
					ptr3 = ptr3->next;
				}
			} 
			return l3;
		}
};

第二次换了一下思路,不管长度是否一致,都在短的数字后面加上一个零,方便与长的数字进行后续相加,也要判断后面是否为NULL

class Solution {
	public:
		ListNode* addTwoNumbers(ListNode* l1,ListNode* l2) {
			if (l1==NULL||l2==NULL) {
				return NULL;
			}
			ListNode* ptr1 = l1;
			ListNode* ptr2 = l2;
			ListNode* l3 = new ListNode(0);
			ListNode* ptr3 = l3;
			while(ptr3!=NULL) {
				ptr3->val=ptr1->val+ptr2->val;
				if(ptr1->next==NULL && ptr2->next!=NULL)
					ptr1->next=new ListNode(0);
				else if(ptr2->next==NULL && ptr1->next!=NULL)
					ptr2->next=new ListNode(0);
				else if(ptr1->next==NULL&&ptr2->next==NULL) {
					if(ptr3->val==0)
						break;
					ptr1->next=new ListNode(0);
					ptr2->next=new ListNode(0);
				}
				ptr1 = ptr1->next;
				ptr2 = ptr2->next;
				ptr1->val = ptr1->val + ptr3->val/10;
				ptr3->val = ptr3->val%10;
				if(ptr1->val+ptr2->val != 0)
					ptr3->next = new ListNode(0);
				else if(ptr1->next!=NULL || ptr2->next!=NULL)
					ptr3->next = new ListNode(0);
				ptr3 = ptr3->next;
			}
			return l3;
		}
};

你可能感兴趣的:(leetcode,leetcode)