Multiply Strings Add Binary Sum of Two Integers Add Strings Add Two Numbers II 参考资料: https://leetcode.com/problems/add-two-numbers/ https://leetcode.com/problems/add-two-numbers/discuss/997/c%2B%2B-Sharing-my-11-line-c%2B%2B-solution-can-someone-make-it-even-more-concise LeetCode ...
publicclassSolution { publicListNode addTwoNumbers(ListNode l1, ListNode l2) { //如果给出就为空,则直接返回另外一个链表 if(l1 ==null)returnl2; if(l2 ==null)returnl1; intflag =0;//存放进位信息,但是并不是处理最后的进位标志 //构造返回结果的第一个节点 ListNode result =newListNode((l1.val ...
*/classSolution{public:ListNode*addTwoNumbers(ListNode*l1,ListNode*l2){autoholder=newListNode();autocur=holder;intcarry=0;while(l1!=NULL||l2!=NULL||carry>0){autol1v=l1!=NULL?l1->val:0;autol2v=l2!=NULL?l2->val:0;autovalue=(l1v+l2v+carry)%10;carry=(l1v+l2v+carry)/10;cur->nex...
final_listnode = Solution().addTwoNumbers(l1.next, l2.next) print(final_listnode) 执行效率也是不错。
Java解决方案 public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead...
public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummyHead = new ListNode(0); if(l1 == null && l2 == null){ return dummyHead; } int sum = 0, carry = 0; ListNode curr = dummyHead;
class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int i1, i2, bi=0, sum=0; ListNode rl = new ListNode(0); ListNode rnow = rl; boolean sig = true; while(true) { if(l1 != null) { i1 = l1.val; ...
Java 代码如下: class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode dummy = new ListNode(0); ListNode cur = dummy; int carry = 0; while (l1 != null || l2 != null || carry != 0) { int i1 = l1 != null ? l1.val : 0; ...
以下是我写的java程序: 一、常规做法: 逐一抽取计算,并考虑其中某个到达链尾的情况。 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(in x) { val = x; } * } */publicclassSolution{public ListNodeaddTwoNumbers(ListNode l1,ListNode...
publicclassSolution {publicstaticListNodeaddTwoNumbers(ListNodel1, ListNodel2) {ListNoderesult=newListNode();ListNodecur=result;intaddOne=;while (l1!=null||l2!=null) {intsum=;sum+=addOne;if (l1!=null) {sum+=l1.val;l1=l1.next; }if (l2!=null) {sum+=l2.val;l2=l2.next; }add...