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. python入门22 pymssql模块(python连接sql server查询)

    安装 pip install pymssql 连接数据库 pymssql.connect() # coding:utf-8 import pymssql server = '192.168.8.1' ...

  2. PHP使用memcache长连接作为RPC客户端需要注意的地方

    memcache扩展版本 3.0.8 一. retry_interval $retry_interval 某个rpc服务器端失败后故障转移的时间,retry_interval的时间内,该节点会被一直标 ...

  3. NTP原理初步与配置

    一.Ntp基本原理 Server和·Client之间的同步(C/S模式) 1.主机启动ntp daemon 2.Client向NTP Server 发送调较时间的申请 3.NTP Server发送标准 ...

  4. 课堂笔记——循环语句-for

    一.循环:多次执行某段代码. 二.循环四要素: 1.初始条件 2.循环条件 3.状态改变 4.循环体 三.for循环 1.语法: for(初始条件;循环条件;状态改变)       { 循环体 } 2 ...

  5. apache php 与nginx php 的区别

    apache是通过mod_php来解析php nginx是通过php-fpm(fast-cgi)来解析php 1. PHP 解释器是否嵌入 Web 服务器进程内部执行 mod_php 通过嵌入 PHP ...

  6. jsp的4个作用域区别( pageScope、requestScope、sessionScope、applicationScope)

    简单描述 page里的变量没法从index.jsp传递到test.jsp.只要页面跳转了,它们就不见了. request里的变量可以跨越forward前后的两页.但是只要刷新页面,它们就重新计算了.  ...

  7. 【luogu P2341 [HAOI2006]受欢迎的牛】 题解

    题解报告:https://www.luogu.org/problemnew/show/P2341 我们把图中的强连通分量缩点,然后只有出度为0的牛是受欢迎的,这样如果出度为0的牛只有一个,说明受所有牛 ...

  8. U盘安装CentOS7.3教程

    0.准备工作: 一台没系统的普通电脑u盘一个(大于1G,最小安装的话不超过1G,根据选择系统大小匹配U盘即可)CentOS7.3 iso文件一个UltraISO工具 1.制作U盘 ①使用UltraIS ...

  9. 课时90.div和span(掌握)

    为什么在这里讲解div和span呢,而不在html中讲解呢? 因为在我们的开发中div和span一般是配合css来使用的,来完成一定的效果,来设置一些属性,在前面我们没有学习css,所以体会不到它的效 ...

  10. SpringBoot非官方教程 | 第九篇: springboot整合Redis

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springboot9-redis/ 本文出自方志朋的博客 这篇文章主 ...