Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)

题目: 给出两个排序的单链表,合并两个单链表,返回合并后的结果;

解题思路:

解法还是很简单的,但是需要注意以下几点:

1.  如果两个链表都空,则返回null;

2.  如果链表1空,则返回链表2的头节点;反之,如果链表2为空,则返回链表1的头节点;

3.  两个链表都不空的情况下:

比较两个链表的头节点的值,哪个小,则新链表的头节点为哪个;

举例:l1: 1->3->5; l2:2->4->6->7;则:head = l1的头节点,此时head.next = l1.next 或者 l2

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; }
 7  * }
 8  */
 9 public class Solution {
10     public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
11         if(l1 == null && l2 == null)
12             return null;
13         if(l1 == null)
14             return l2;
15         if(l2 == null)
16             return l1;
17         ListNode head = null;
18         if(l1.val > l2.val)
19         {
20              head = l2;
21              head.next = mergeTwoLists(l1, l2.next);
22         }
23         else
24         {
25              head = l1;
26              head.next = mergeTwoLists(l1.next, l2);
27         }
28         return head;
29     }
30 }
时间: 2024-10-14 10:30:04

Leetcode21--->Merge Two Sorted Lists(合并两个排序的单链表)的相关文章

[LeetCode]80. 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. Subscribe to see which companies asked this question 解法1:递归.首先比较头节点大小,若l2->val>l1->val,则返回mergeTwoLists(

Leetcode23--->Merge K sorted Lists(合并k个排序的单链表)

题目: 合并k个排序将k个已排序的链表合并为一个排好序的链表,并分析其时间复杂度 . 解题思路: 类似于归并排序的思想,lists中存放的是多个单链表,将lists的头和尾两个链表合并,放在头,头向后移动,尾向前移动,继续合并,直到头和尾相等,此时已经归并了一半, 然后以同样的方法又重新开始归并剩下的一半.时间复杂度是O(logn),合并两个链表的时间复杂度是O(n),则总的时间复杂度大概是O(nlogn):合并两个单链表算法可以参考Leetcode21中的解法:http://www.cnblo

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. 代码如下: * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x

[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 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link

LeetCode 21. Merge Two Sorted Lists合并两个有序链表 (C++)

题目: 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: Input: 1->2->4, 1->3->4 Output: 1->1->2->3->4->4 分析: 将两个有序链表合并为一个新的有序链表并返

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* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode v(0),*pre = &v,*p

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

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6 要合并K个排好序的链表,我用的方法是用一个优先队列每次存K个元素在队列中,根据优

合并两个排序的单链表

[题目] 输入两个递增排序的链表,合并这两个链表并使新链表中的节点仍然是依照递增排序的. [分析] 合并单链表,须要找到头结点,对照两个链表头结点后,确定头结点,再确定头结点下一个结点,循环递归的如前面一样操作确定每一个结点位置,同一时候考虑边界条件,假设两个链表为空.则肯定无需合并了,就是空链表,假设一个链表为空,还有一个不为空,则返回不为空的链表.详细分析流程能够看以下的样例: [測试代码] #include<stdio.h> #include<stdlib.h> #inclu