Merge Two Linked List
2016-02-03 23:36
295 查看
Given 1-3-5-6-8, 2-4-9, you want to merge to 1-2-3-4-5-6-8-9.
My solution is very naive solution that actually build a new linkedlist, require space O(n).
My solution is very naive solution that actually build a new linkedlist, require space O(n).
public ListNode mergeTwoLists(ListNode l1, ListNode l2){ ListNode fakeHead = new ListNode(0); ListNode cur = fakeHead; while(l1!=null && l2!=null){ if(l1.val > l2.val){ cur.next = l2; l2 = l2.next; } else{ cur.next = l1; l1 = l1.next; } cur = cur.next; } //还要考虑剩下没完的list,比如1-2-3和9-10,cur=1-2-3,但是9-10还没动呢,要再来个cur.next = l1/2。 if(l1!=null){ cur.next = l1; } else{ cur.next = l2; } return fakeHead.next; }
相关文章推荐
- 自己都找不到自己
- 杭电1017 A Mathematical Curiosity
- Web开发进阶之Servlet理解
- Laravel 5.2 使用illuminate/html 报错的问题
- 欢迎使用Markdown编辑器写博客
- 例题6-2 UVA 514 - Rails (铁轨)
- 概率DP合集
- 简述Linq中.ToList(), .AsEnumerable(), AsQueryable()的区别和用法
- php魔术方法
- MongoDB_索引
- phpinfo() 中 Local Value(局部变量)Master Value(主变量) 的区别
- 解决Android Activity切换时出现白屏问题
- Java Mail简介
- 【慕课笔记】U3 继承 第3节 JAVA中的继承初始化顺序
- leetcode32. Longest Valid Parentheses
- 最简单的基于FFmpeg的AVDevice例子(屏幕录制)
- Markdown
- Android Material Design控件学习(三)——使用TextInputLayout实现酷市场登录效果
- TCP与IP协议的相互作用
- linux route路由