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 val;
 3     ListNode *next;
 4     ListNode(int x):val(x), next(NULL) {}
 5 };
 6
 7 ListNode *GetLists(int n)    //得到一个列表
 8 {
 9     ListNode *l = new ListNode(0);
10     ListNode *pre = l;
11     int val;
12     for (int i = 0; i < n; i ++) {
13         cin >> val;
14         ListNode *newNode = new ListNode(val);
15         pre->next = newNode;
16         pre = pre->next;
17     }
18     return l->next;
19 }
20
21 ListNode *mergeTwoLists(ListNode *l1, ListNode *l2)
22 {
23     assert (NULL != l1 && NULL != l2);
24     if (NULL == l1 && NULL == l2)
25         return NULL;
26     if (NULL == l1 && NULL != l2) // !!要记得处理一个为空,另一个不为空的情况
27         return l2;
28     if (NULL != l1 && NULL == l2)
29         return l1;
30
31     ListNode *temp = new ListNode(0);
32     temp->next = l1;
33     ListNode *pre = temp;
34
35     while(NULL != l1 && NULL != l2) {
36         if (l1->val > l2->val) { //从小到大排列
37             ListNode *next = l2->next;
38             l2->next = pre->next;
39             pre->next = l2;
40             l2 = next;
41         }
42         else {
43             l1 = l1->next;
44         }
45         pre = pre->next;
46     }
47     if (NULL != l2) {
48         pre->next = l2;
49     }
50     return temp->next;
51 }

这其中要注意一点,即要记得处理一个链表为空,另一个不为空的情况,如{}, {0} -- > {0},当然上面的写法多少啰嗦了一些,可以简写。

时间: 2024-11-05 16:08:51

LeetCode:Merge Two Sorted Lists的相关文章

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 21 23:Merge Two Sorted Lists &amp; 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 k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

Java for LeetCode 023 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路一: 之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大.如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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. Example Example: Input: 1->2->4, 1-&g

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个链表的第一个节点集合,建堆,然后取出堆顶的节点,链接到结果链表上,然后将该节点的下一个节点入堆,直到所有