Leetcode: Add Two Numbers

原创 2016年08月31日 16:21:59
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
  		ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
		if (l1 == NULL){
			return l2;
		}
		if (l2 == NULL){
			return l1;
		}
		ListNode *ans = l1;
		int cur = 0;
		ListNode *p=l1;
		while (l1 && l2){
			int temp = l1->val + l2->val + cur;
			l1->val = temp % 10;
			cur = temp / 10;
			p = l1;
			 l2 = l2->next;
			 if (l1->next == NULL && l2!=NULL){
				 l1->next = new ListNode(0);
				 l1 = l1->next;
				 break;
				
			 }
			 l1 = l1->next;
		}
		if (l1 == NULL && l2 == NULL && cur){
			ListNode * ff = ans;
			while (ff->next != NULL){
				ff = ff->next;
			}
			ff->next = new ListNode(1);
		
		}

		else if (l2!=NULL){
		while (l2 != NULL){
			int temp = cur + l2->val;

			l1->val = temp % 10;
			cur = temp / 10;
			l2 = l2->next;
			p = l1;
		
			l1->next = new ListNode(0);
			l1 = l1->next;

		 }
    		 if (cur){
    		  l1->val = 1;
    	  }
		}
		else if (l1!=NULL)
	  while(l1 != NULL){
		
			int temp = cur  + l1->val;
			l1->val = temp % 10;
			cur = temp /= 10;
			p = l1;
		
			if (l1->next == NULL &&  cur){
				l1->next = new ListNode(cur);
				l1->next->val = cur;
				l1 = l1->next;
				break;

			}
			
				l1 = l1->next;

	  }
	//  cout << ans->val << " " << ans->next->val << endl;
	  //cout << "fuck" << p->val << endl;
      
	 if (p->next && p->next->val == 0){
		  p->next = NULL;
	  }
		return ans;
	}
};

版权声明:本文为博主原创文章,欢迎指教~

【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】

有两个单链表,代表两个非负数,每一个节点代表一个数位,数字是反向存储的,即第一个结点表示最低位,最后一个结点表示最高位。求两个数的相加和,并且以链表形式返回。对两个链表都从第一个开始处理,进行相加,结...

【LeetCode编程学习(C语言)】2.Add Two Numbers

LeetCode第二题Add Two Numbers的C语言实现
  • yake827
  • yake827
  • 2016年03月29日 13:54
  • 998

[LeetCode] Add two numbers

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode...

LeetCode 2 - Add Two Numbers

问题简述 Add Two Numbers You are given two linked lists representing two non-negative numbe...

[LeetCode][2]Add Two Numbers解析 -Java实现

Q: You are given two linked lists representing two non-negative numbers. The digits are stored in re...

LeetCode 开刷2 ADD Two Numbers

趁着还有热情,接着刷。 题目: You are given two linked lists representing two non-negative numbers. The digits ...

leetcode Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in rever...

LeetCode 2. Add Two Numbers (C++/python/js实现)

将链表所代表的两个数相加,注意维护仅为

[leetcode]Add Two Numbers

Difficult:Medium You are given two non-empty linked lists representing two non-negative integers. Th...

LeetCode(2)Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in...
  • fly_yr
  • fly_yr
  • 2015年04月23日 20:48
  • 758
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Leetcode: Add Two Numbers
举报原因:
原因补充:

(最多只允许输入30个字)