No.021:Merge Two Sorted Lists

问题:

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.

官方难度:

Easy

翻译:

合并2个已排序的链表,得到一个新的链表并且返回其第一个节点。

  1. 考虑输入节点存在null的情况,直接返回另一个节点。
  2. 节点的定义在No.002(Add Two Numbers)中有定义。
  3. 记录上一个节点last,保留第一个节点first。
  4. 以其中一条链表为基准,作为待排序的链表,其第一个节点记为l1。比较l1和l2的value值,如果l1的value是较大的一方,交换l1和l2节点,保证last节点的next指针指向l1节点。
  5. 当l1节点的next指针为null时,表明第一条链表完全有序,且最后一个节点的值小于另一条链表第一个节点的值,连接这两个节点,整条链表自然有序。

解题代码:

 1     public static ListNode mergeTwoLists(ListNode l1, ListNode l2) {
 2         if (l1 == null) {
 3             return l2;
 4         }
 5         if (l2 == null) {
 6             return l1;
 7         }
 8         // 返回的第一个节点
 9         ListNode first = l1.val > l2.val ? l2 : l1;
10         // 上一个节点
11         ListNode last = new ListNode(0);
12         while (true) {
13             if (l1.val > l2.val) {
14                 // 交换l1节点和l2节点,保证下一次循环仍然以l1节点为基准
15                 ListNode swapNode = l2;
16                 l2 = l1;
17                 l1 = swapNode;
18             }
19             // 将last节点的next指针指向l1,同时更新last
20             last.next = l1;
21             last = l1;
22             // 带排序的链表遍历完成,剩余链表自然有序
23             if (l1.next == null) {
24                 l1.next = l2;
25                 break;
26             }
27             l1 = l1.next;
28         }
29         return first;
30     }

mergeTwoLists

相关链接:

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

https://github.com/Gerrard-Feng/LeetCode/blob/master/LeetCode/src/com/gerrard/algorithm/easy/Q021.java

PS:如有不正确或提高效率的方法,欢迎留言,谢谢!

时间: 2024-10-12 16:52:43

No.021:Merge Two Sorted Lists的相关文章

LeetCode 21 23:Merge Two Sorted Lists & Merge K Sorted Lists

LeetCode 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 together the nodes of the first two lists. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

LeetCode:Merge Two Sorted Lists

题目:Merge Two Sorted Lists 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. 简单题,只要对两个链表中的元素进行比较,然后移动即可,只要对链表的增删操作熟悉,几分钟就可以写出来,代码如下: 1 struct ListNode { 2 int

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

LeetCode 23: Merge K Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 本题在上一题(LeetCode 21: Merge Two Sorted Lists)基础上再加深了一步,链表个数从两个改为K个. 此题有如下几种解法: 1.最简单的方法莫过于每次都从K个链表头中找到最小的那个元素,加入结果链表中.基于此,有人通过最小堆来简化最小元素的比较. struct Compa

LeetCode之“链表”:Merge Two Sorted Lists && Merge k Sorted Lists

1. Merge Two Sorted Lists 题目链接 题目要求:  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. 这道题目题意是要将两个有序的链表合并为一个有序链表.为了编程方便,在程序中引入dummy节点.具体程序如下: 1 /** 2 * Defin

LeetCode OJ:Merge k Sorted Lists(归并k个链表)

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 类似于归并2个链表,暴力一点的方法就是,每取出一个list就与以前的list归并返回merge后list,知道所有list merge完成. 但是可惜,这样做会TLE.贴下代码先: 1 /** 2 * Definition for singly-linked list. 3 * struct List

LeetCode OJ:Merge Two Sorted Lists(合并两个链表)

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. 注意题目要求合并的时候不能新建节点,直接使用原来的节点,比较简单,代码如下: 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4

leetcode笔记:Merge Two Sorted Lists

一.题目描述 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. 二.题目分析 这道题题意是,将两个已排序的链表的各个元素进行比较并合并成一个链表,具体思路是,当某一个列表的元素比较小的话,就将其加入到输出链表中,并将该链表的指针指向链表的下一个元素.题目需要注意边界

LeetCode 023 Merge k Sorted Lists

题目要求:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 分析: 参考网址:http://blog.csdn.net/a83610312/article/details/8554241 思路是:首先将k个链表的第一个节点集合,建堆,然后取出堆顶的节点,链接到结果链表上,然后将该节点的下一个节点入堆,直到所有