/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode AddTwoNumbers(ListNode l1, ListNode l2) {
if(l1 == null || l2 == null) return l1;
ListNode head = new ListNode(0);//return head.next later
ListNode current = head;
int flag = 0;
while(l1 != null || l2 != null || flag == 1){
int temp = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + flag;
current.next = new ListNode(temp % 10);
flag = temp / 10;
current = current.next;
l1 = l1 != null ? l1.next : l1;
l2 = l2 != null ? l2.next : l2;
}
return head.next;
}
}
2. Add Two Numbers
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- ## 题目 >Add Two Numbers You are given two linked lists rep...
- 从今天开始,写一下我在刷 LeetCode 时的心得体会,包括自己的思路和别人的优秀思路,欢迎各种监督啊! ...
- You are given two non-empty linked lists representing two...