leetcode笔记
来源:互联网 发布:阿里算法 编辑:程序博客网 时间:2024/06/05 16:19
语言:java
第二题
You are given two linked lists representing two non-negative numbers. 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 linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { int next=0; ListNode r= new ListNode(0); ListNode nextPt= r; ListNode tl1=l1; ListNode tl2=l2; while(tl1!=null||tl2!=null){ int val1=tl1!=null?tl1.val:0; int val2=tl2!=null?tl2.val:0; int sum=val1+val2+nextPt.val; nextPt.val=sum%10; if(sum>9){ next=1; } if(tl1!=null) tl1=tl1.next; if(tl2!=null) tl2=tl2.next; if(tl1==null&&tl2==null&&next==0){ nextPt.next=null; }else{ nextPt.next=new ListNode(next); next=0; nextPt=nextPt.next; } } return r; }}
0 0
- leetcode笔记
- Leetcode 笔记
- LeetCode笔记
- leetcode 笔记
- leetcode笔记
- LeetCode 笔记
- 【LeetCode】Leetcode刷题 笔记
- 【LeetCode笔记】Reverse Integer
- 【LeetCode笔记】Word Break
- 【LeetCode笔记】Candy
- 【LeetCode笔记】Palindrome Partitioning
- 【LeetCode笔记】Merge Intervals
- 【LeetCode笔记】Rotate Array
- leetcode个人笔记
- leetcode笔记:2Sum
- leetcode笔记:3Sum
- leetcode笔记:4Sum
- LeetCode学习笔记
- 【问题解决】无法定位程序输入点~于动态链接库上(The procedure entry point ~ could not be located)
- servlet3.0 新特性——异步处理
- html乱码原因与网页与网页乱码解决方法
- iOS开发使用UIWebView显示多种文档
- 全局变量的危害(转发)
- leetcode笔记
- Json解析的感悟
- 论文提要“You Only Look Once: Unified, Real-Time Object Detection”
- JAF介绍
- 代码实现drawable selector效果
- CoordinatorLayout的简单使用
- Android编程开发规范
- Python/C API 抽象对象层(部分)
- 图形算法 - 模糊函数比较,Blur Function Compare。