Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

Show Tags

SOLUTION 1:

使用dummynode记录头节点的前一个,轻松完成,2分钟就AC啦!

 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode();
ListNode cur = dummy; while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
} if (l1 != null) {
cur.next = l1;
} else {
cur.next = l2;
} return dummy.next;
}
}

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/list/MergeTwoLists_1206.java

最新文章

  1. 【HDU2255】奔小康赚大钱-KM算法
  2. (转)关于Oracle AUTONOMOUS TRANSACTION(自治事务)的介绍
  3. 匈牙利 算法&amp;模板
  4. Perl Debug error: SetConsoleMode failed, LastError=|6|
  5. 在ACCESS中创建数据库和查询(ACCESS 2000)
  6. NYOJ-744蚂蚁的难题(一)
  7. GUI编程(一)-----概述
  8. [Cocos2d-x]CCSpriteBatchNode的使用
  9. 激活OFFICE2010时,提示choice.exe不是有效的win32程序
  10. SmartCoder每日站立会议09
  11. python集合使用范例的代码
  12. 【Python】函数总结
  13. windows下JDK环境配置与Android SDK环境配置
  14. cookie和session的关联关系
  15. pymongo
  16. Canvas 3D球形文字云动画特效
  17. golang包time用法详解
  18. map set iterator not incrementable 解决办法
  19. ESXi时间同步
  20. SharePoint自动化系列——Upload files to SharePoint library using PowerShell.

热门文章

  1. android开发学习---linux下开发环境的搭建&amp;&amp; android基础知识介绍
  2. Xcode 8 的 Debug 新特性 —- WWDC 2016 Session 410 &amp; 412 学习笔记
  3. 如何在windows下安装JDK
  4. python练习笔记——面试题 F(n) = F(n-1)+F(n-2)
  5. golang学习笔记 --switch
  6. UI--仿IOS控件之ActionSheet样式 and more..
  7. Python 文件 write() 方法
  8. Source Insight中代码块注释
  9. 【转载并整理】JAVA解析或生成xml的四种方法
  10. svm工具箱快速入手简易教程