代码实现: 1classSolution {2public:3ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {4ListNode preHead(0), *p = &preHead;5intcarry =0;6while(l1 || l2 ||carry) {7intsum = (l1 ? l1->val :0) + (l2 ? l2->val :0) +carry;8carry = sum /10;9p->next =newListNode(sum %10);10p = p->next...
LeetCode-Add Two Numbers 没有难度的题目,纯用来练习链表操作和递归的使用的,难得的一次就能ac大小数据呀! 1classSolution {2public:3ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {4//Start typing your C/C++ solution below5//DO NOT write int main() function6ListNode *res = add(l1, l2,...
1classSolution {2public:3ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {4ListNode *res =newListNode(-1);5ListNode *cur =res;6intcarry =0;7while(l1 ||l2) {8intn1 = l1 ? l1->val :0;9intn2 = l2 ? l2->val :0;10intsum = n1 + n2 +carry;11carry = sum /10;12cur->...
本题是 Leetcode Top 100 liked questions 中的第二题。 2. Add Two Numbers Medium 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...
class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: link1 = l1 link2 = l2 while(link1!=None and link2 !=None): a = link1.val link2.val += a link1.val = link2.val link1 = link1.next
class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { // Start typing your C/C++ solution below if(l1 == NULL && l2 == NULL) { return NULL; } ListNode *retList = new ListNode(0); ListNode *tmpList = retList; ...
public class Solution { /* * 方法1 */ public static ListNode addTwoNumbers(ListNode l1,ListNode l2) { //如果都为空 直接返回不为空的一个参数 如果都未空 则返回空 if(l1 == null || l2 == null){ return l1 == null ?(l2 == null ?null:l2):l1; ...
// LeetCode, Add Two Numbers // 时间复杂度 O(m+n),空间复杂度 O(1) classSolution{public:ListNode*addTwoNumbers(ListNode*l1,ListNode*l2){ListNodedummy(-1);// 头节点intcarry=0;ListNode*prev=&dummy;for(ListNode*pa=l1,*pb=l2;pa!=nullptr||pb!=nullptr;pa=pa==nullptr?nullptr:pa->next,...
{ * val = x; * next = null; * } * } */ public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { if(l1==null) return l2; if(l2==null) return l1; ListNode node1=l1; ListNode node2=l2; int a =0,b=0; int c=1; while(node1!=null) { a+=(c*...
# class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def addTwoNumbers(self, l1, l2): """ :type l1: ListNode :type l2: ListNode :rtype: ListNode """ head = q =ListNode(-1) carry = 0 while l1 or l2 or carry:...