LeetCode: Merge Two Sorted Lists 解题报告
2024-08-29 18:19:46
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
最新文章
- 【HDU2255】奔小康赚大钱-KM算法
- (转)关于Oracle AUTONOMOUS TRANSACTION(自治事务)的介绍
- 匈牙利 算法&;模板
- Perl Debug error: SetConsoleMode failed, LastError=|6|
- 在ACCESS中创建数据库和查询(ACCESS 2000)
- NYOJ-744蚂蚁的难题(一)
- GUI编程(一)-----概述
- [Cocos2d-x]CCSpriteBatchNode的使用
- 激活OFFICE2010时,提示choice.exe不是有效的win32程序
- SmartCoder每日站立会议09
- python集合使用范例的代码
- 【Python】函数总结
- windows下JDK环境配置与Android SDK环境配置
- cookie和session的关联关系
- pymongo
- Canvas 3D球形文字云动画特效
- golang包time用法详解
- map set iterator not incrementable 解决办法
- ESXi时间同步
- SharePoint自动化系列——Upload files to SharePoint library using PowerShell.
热门文章
- android开发学习---linux下开发环境的搭建&;&; android基础知识介绍
- Xcode 8 的 Debug 新特性 —- WWDC 2016 Session 410 &; 412 学习笔记
- 如何在windows下安装JDK
- python练习笔记——面试题 F(n) = F(n-1)+F(n-2)
- golang学习笔记 --switch
- UI--仿IOS控件之ActionSheet样式 and more..
- Python 文件 write() 方法
- Source Insight中代码块注释
- 【转载并整理】JAVA解析或生成xml的四种方法
- svm工具箱快速入手简易教程