1. 原题链接

https://leetcode.com/problems/merge-two-sorted-lists/description/

2. 题目要求

给出两个已经从小到大排序的链表ls1、ls2,进行合并,合并后仍有序,返回合并后的链表

3. 解题思路

创建一个表头指针headPointer和一个定位指针locatePointer,headPointer用来保存头结点。

同时对ls1和ls2进行遍历,当ls1的值小于ls2的值时,locatePointer指向ls1,否则指向ls2。

当一个链表为空另一个不为空,则将不为空链表的剩余结点依次加入新的链表。

4. 代码实现

public class MergeTwoSortedList {
public static void main(String[] args) {
ListNode ls1 = new ListNode(-9);
ListNode ls2 = new ListNode(3);
ListNode ls3 = new ListNode(4);
ListNode ls4 = new ListNode(5);
ListNode ls5 = new ListNode(7);
ListNode ls6 = new ListNode(8);
ls1.next = ls2;
ls2.next = ls3;
ls4.next = ls5;
ls5.next = ls6; ListNode ls = mergeTwoLists(ls1, ls4);
while (ls != null) {
System.out.println(ls.val);
ls = ls.next;
}
} public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode headPointer = new ListNode(-1);
ListNode res = headPointer; while (l1 != null || l2 != null) { if (l1 != null && l2 == null) {
res.next = l1;
l1 = l1.next;
} else if (l1 == null && l2 != null) {
res.next = l2;
l2 = l2.next;
} else {
if (l1.val < l2.val) {
res.next = l1;
l1 = l1.next;
} else {
res.next = l2;
l2 = l2.next;
}
}
res = res.next;
} return headPointer.next;
} public static class ListNode {
int val;
ListNode next; ListNode(int x) {
val = x;
}
}
}

  

LeetCode:21. Merge Two Sorted Lists(Easy)的更多相关文章

  1. Leetcode 21. Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  2. 【leetcode】Merge Two Sorted Lists(easy)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  3. 21.Merge Two Sorted Lists(链表)

    Merge two sorted linked lists and return it as a new list. The new list should be made by splicing t ...

  4. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  5. leetCode练题——21. Merge Two Sorted Lists(照搬大神做法)

    1.题目 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  6. [Leetcode][Python]21: Merge Two Sorted Lists

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 21: Merge Two Sorted Listshttps://oj.le ...

  7. 【一天一道LeetCode】#21. Merge Two Sorted Lists

    一天一道LeetCode系列 (一)题目 Merge two sorted linked lists and return it as a new list. The new list should ...

  8. Leetcode练习题21. Merge Two Sorted Lists

    题目描述(easy) Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new ...

  9. 【LeetCode】21. Merge Two Sorted Lists 合并两个有序链表

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:合并,有序链表,递归,迭代,题解,leetcode, 力 ...

随机推荐

  1. groupadd

    功能说明:用于创建新的用户组. 参数选项:-g gid 指定用户组的gid,除非接-o参数,否则ID值唯一且不为负,如果不指定-g参数,则gid从500开始.-f 新增一个账户,强制覆盖一个已存在的组 ...

  2. 【HHHOJ】ZJOI2019模拟赛(十六)4.07 解题报告

    点此进入比赛 得分: \(100+100+100=300\) 排名: \(Rank\ 1\) \(Rating\): \(+13\)(\(\frac18Rated\)) 备注: 这场比赛全是做过的原题 ...

  3. CFS调度分析(内核版本:2.6.34)

    CFS调度分析(内核版本:2.6.34) 1.时间记账 CFS不再有时间片的概念,他维护的是每个进程运行的时间记账 使用调度器实体结构来追踪进程运行记账: <linux/sched.h> ...

  4. 2018.11.28 OGNL表达式与struts2框架结合的体现---在配置文件中体现(补充)

    Demo3Action配置 struts.xml 主配置文件配置 地址栏输入 http://localhost:8080/StrutsDay03/Demo3Action 对于主配置的文件参数而言,如果 ...

  5. Yii2 指定log存放的文件

    在main.php 文件里添加 'log' => [ 'traceLevel' => YII_DEBUG ? 3 : 0, 'targets' => [ [ 'class' => ...

  6. Ajax去除缓存

    1.在Ajax发送请求钱加上anyAjaxObj.setRequestHeader("If-Modeified-Since","0").2.在Ajax发送请求钱 ...

  7. python—递归函数

    递归函数 定义:即在函数定义中自己调用自己 递归就是在过程或函数中自我调用 递归必须有递归出口,即递归结束条件 举个栗子-阶乘: def fact(n): if n == 1: return 1 re ...

  8. Services 在多个 controller 中共享数据。

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  9. jsp页面的传值(list)

    jsp页面与xml文件对应的关系: 例:网页上jsp的url为----purchase_app_btn.do? 对应xml文件下的 <action path="/purchase_ap ...

  10. 《瞿葩的数字游戏》T3-三角圣地(Lucas)

    题目背景 国王1带大家到了数字王国的中心:三角圣地. 题目描述 不是说三角形是最稳定的图形嘛,数字王国的中心便是由一个倒三角构成.这个倒三角的顶端有一排数字,分别是1~N.1~N可以交换位置.之后的每 ...