Leetcode-002-涓ゆ暟鐩稿姞
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode-002-涓ゆ暟鐩稿姞相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/reader' title='reader'>reader link while public span 鎿嶄綔 leetcode read
鏈涓昏鏄€冩煡瀵归摼琛ㄧ殑鎿嶄綔銆?/p>
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode cur1 = l1; ListNode cur2 = l2; int temp = 0; ListNode init = new ListNode(0); ListNode cur = init; while(cur1!=null || cur2!=null){ temp/=10; if(cur1!=null){ temp += cur1.val; cur1 = cur1.next; } if(cur2!=null){ temp += cur2.val; cur2 = cur2.next; } cur.next = new ListNode(temp%10); cur = cur.next; } if(temp/10!=0){ cur.next = new ListNode(1); } return init.next; } }
# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: cur1 = l1 cur2 = l2 init = ListNode(0) cur = init temp = 0 while cur1 is not None or cur2 is not None: temp //= 10; if cur1 is not None: temp+=cur1.val cur1 = cur1.next if cur2 is not None: temp+=cur2.val cur2 = cur2.next cur.next = ListNode(temp%10) cur = cur.next if temp//10!=0: cur.next = ListNode(1) return init.next
以上是关于Leetcode-002-涓ゆ暟鐩稿姞的主要内容,如果未能解决你的问题,请参考以下文章
銆愯绋嬭璁★綔C++銆慍++绋嬪簭璁捐鍩虹锛氱煩闃佃繍绠楀櫒