题目:
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]相关推荐

  1. LeetCode刷题记录15——21. Merge Two Sorted Lists(easy)

    LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) 目录 LeetCode刷题记录15--21. Merge Two Sorted Lists(easy) ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

最新文章

  1. VS2010 win32项目windows窗体程序 向导生成代码解析
  2. 策略模式在JDK 源码中的体现
  3. 如何根据jar包的名称找到包含对应源代码的jar包
  4. 一键多功能按键识别c语言,单片机一键多功能按键识别设计
  5. 关机时长时间停留在”正在保存设置“的解决办法
  6. 数据库索引如何工作? [关闭]
  7. 【学习笔记】《光纤传感器振动系统信号解调技术研究--华北电力--控制工程--张**》重点笔记
  8. 微信小程序下拉刷新事件无效原因
  9. 羊年祝福语(羊年祝福大全)
  10. 我和《独角兽项目》背后的故事
  11. 相机取相,如何给类似圆柱的侧面打光均匀
  12. 康考迪亚大学 计算机科学,康考迪亚大学 Concordia University
  13. echarts饼状图中间显示数值加上名字
  14. 《深入解析Windows操作系统》之系统机制
  15. 如何免费创建一个自己的网站(可通过外网访问)
  16. 西行漫记(12):对象,结束与开始
  17. PRO-1:简易智能停车系统
  18. Linux网卡驱动(3)-网卡驱动深层分析
  19. Linux基础和c语言的高级用法
  20. android 自定义view实现推箱子小游戏。

热门文章

  1. pcb地线应该不应该做成环路_干货|图文详解EMC的PCB设计技术
  2. Python基础02-序列及通用操作
  3. Dominant Indices(CF 1009 F)
  4. Chromium Android编译指南
  5. frida 安装特定版本
  6. Netty学习笔记(六)Pipeline的传播机制
  7. Go 内存对齐的那些事儿
  8. 彷徨编程十几年,终于盯上 Rust
  9. 【视频编解码的新挑战与新机会】
  10. 微信AI从识物到通用图像搜索的探索揭秘