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. Twitter Typeahead plugin Example

    原文网址: http://dhtmlexamples.com/2012/02/27/using-the-typeahead-plugin-with-twitter-bootstrap-2-and-jq ...

  2. One Order行项目里Item Category是怎么计算出来的

    One Order的行项目里有个字段叫Item Category,我们在行项目里加入一个product后,就会自动带出Item Category来.这个值是怎么计算出来的? 检查CRMD_ORDERA ...

  3. Fiori Launchpad Tile点击后跳转的调试技巧

    在SAP Fiori launchpad 里点击某个tile之后,后台会计算出跳转的目标url返回给前台. 下图中一个个白色的方框就成为tile.每个tile点击之后,会打开一个对应的Fiori应用. ...

  4. Android学习笔记_72_Spinner的用法

    一.普通 1. <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android= ...

  5. Dependency Injection in ASP.NET Web API 2 (在web api2 中使用依赖注入)

    原文:http://www.asp.net/web-api/overview/advanced/dependency-injection 1 什么是依赖注入(Dependency Injection) ...

  6. Vue开发 localhost 替换成 本机ip无法访问

    新版 vue-cli(@3.10.10) 构建的项目.localhost 替换成本机 ip 地址之后无法访问.但是替换成 127.0.0.1 可以访问 找到 config 文件夹下面的 index.i ...

  7. 使用补丁破解IntelliJ IDEA 2017收费版本(转)

    1. 首先去官网http://www.jetbrains.com/idea/download/#section=windows下载Ultimate版(注意不是community版)下载并安装.一定要记 ...

  8. 菜鸟笔记 -- Chapter 6.4 面向对象的三大特性

    6.4.1  三大特性概述 面向对象的三大特性是Java中一个很重要的基本理念. 封装是面向对象的核心思想.将对象的属性和行为封装起来,其载体就是类,类通常对客户隐藏其实现细节,这就是封装的意思.采用 ...

  9. MySql Connector/C++8事务处理Demo

    #include <iostream> #include <exception> #include <vector> #include <unistd.h&g ...

  10. python核心编程2 第十三章 练习

    13-3.对类进行定制.写一个类,用来将浮点型值转换为金额. class MoneyFmt(object): def __init__(self, value=0.0, flag='-'): self ...