Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]
题目:
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.
翻译:
合并2个已经排序的链表,并且返回一个新的链表。这个新的链表应该由前面提到的2个链表的节点所组成。
分析:
注意头节点的处理,和链表结束(next为null)的处理。以下代码新增了一个头指针,来把头节点的处理和普通节点的处理统一了。
代码:
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/
public class Solution {public ListNode mergeTwoLists(ListNode l1, ListNode l2) {ListNode head=new ListNode(0);ListNode currentNode=head;while(true){if(l1==null&&l2==null){break;}else if(l2!=null&&(l1==null||l1.val>l2.val)){currentNode.next=l2;l2=l2.next;}else{currentNode.next=l1;l1=l1.next;}currentNode=currentNode.next;}return head.next;}
}
Leet Code OJ 21. Merge Two Sorted Lists [Difficulty: Easy]相关推荐
- LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)
LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...
- Leet Code OJ 119. Pascal's Triangle II [Difficulty: Easy]
题目: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return ...
- Leet Code OJ 8. String to Integer (atoi) [Difficulty: Easy]
题目: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input ca ...
- Leet Code OJ 203. Remove Linked List Elements [Difficulty: Easy]
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 –> 2 ...
- Leet Code OJ 191. Number of 1 Bits [Difficulty: Easy]
题目: Write a function that takes an unsigned integer and returns the number of '1' bits it has (also ...
- Leet Code OJ 328. Odd Even Linked List [Difficulty: Easy]
题目: Given a singly linked list, group all odd nodes together followed by the even nodes. Please note ...
- Leet Code OJ 58. Length of Last Word [Difficulty: Easy]
题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return t ...
- Leet Code OJ 168. Excel Sheet Column Title [Difficulty: Easy]
题目: Given a positive integer, return its corresponding column title as appear in an Excel sheet. For ...
- Leet Code OJ 171. Excel Sheet Column Number [Difficulty: Easy]
题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, r ...
最新文章
- VS2010 win32项目windows窗体程序 向导生成代码解析
- 策略模式在JDK 源码中的体现
- 如何根据jar包的名称找到包含对应源代码的jar包
- 一键多功能按键识别c语言,单片机一键多功能按键识别设计
- 关机时长时间停留在”正在保存设置“的解决办法
- 数据库索引如何工作? [关闭]
- 【学习笔记】《光纤传感器振动系统信号解调技术研究--华北电力--控制工程--张**》重点笔记
- 微信小程序下拉刷新事件无效原因
- 羊年祝福语(羊年祝福大全)
- 我和《独角兽项目》背后的故事
- 相机取相,如何给类似圆柱的侧面打光均匀
- 康考迪亚大学 计算机科学,康考迪亚大学 Concordia University
- echarts饼状图中间显示数值加上名字
- 《深入解析Windows操作系统》之系统机制
- 如何免费创建一个自己的网站(可通过外网访问)
- 西行漫记(12):对象,结束与开始
- PRO-1:简易智能停车系统
- Linux网卡驱动(3)-网卡驱动深层分析
- Linux基础和c语言的高级用法
- android 自定义view实现推箱子小游戏。