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. 关于GitHubGit

    一.Github项目地址:https://github.com/gyguyt/Helloworld123 二.什么是Github? Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或 ...

  2. MHA 一主一从搭建

    安装基本环境介绍 一主一从     MHA manager & slave  node2 192.168.56.27 Master机器 node1 192.168.56.26 VIP   19 ...

  3. HDU 5258 数长方形【离散化+暴力】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5258 数长方形 Time Limit: 2000/1000 MS (Java/Others)    Me ...

  4. 前端静态文件如何应对HTTPS的到来

    近几年,越来越多的网站开始支持https,我们可以看到国外的比如github.谷歌.facebook:国内的有百度.淘宝.博客园.coding.net.worktile等一系列的网站. 我再最近的开发 ...

  5. Spring知识点总结(五)Spring整合JDBC

     1. 回顾JDBC        a. java操作关系型数据的API.导入相关数据库的驱动包后可以通过JDBC提供的接口来操作数据库.        b. 实现JDBC的六个步骤          ...

  6. 21.Shiro在springboot与vue前后端分离项目里的session管理

    1.前言 当决定前端与后端代码分开部署时,发现shiro自带的session不起作用了. 然后通过对请求head的分析,然后在网上查找一部分解决方案. 最终就是,登录成功之后,前端接收到后端传回来的s ...

  7. 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f1-eureka/ 本文出自方志朋的博客 一.spring ...

  8. oracle权限配置

    系统权限管理:1.系统权限分类:DBA: 拥有全部特权,是系统最高权限,只有DBA才可以创建数据库结构. RESOURCE:拥有Resource权限的用户只可以创建实体,不可以创建数据库结构. CON ...

  9. CocoaPods介绍与使用(转)

    一.介绍 (1) CocoaPods是一个用来帮助我们管理第三方依赖库的工具.它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我们的工程 ...

  10. ABAP术语-Authorization Object

    Authorization Object 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/20/1006585.html Element of ...