# [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.

C++ 解法一：

class Solution {

public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(-1), *cur = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
cur->next = l1;
l1 = l1->next;
} else {
cur->next = l2;
l2 = l2->next;
}
cur = cur->next;
}
cur->next = l1 ? l1 : l2;
return dummy->next;
}
}; 

Java 解法一：

public class Solution {

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(-1), cur = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
cur.next = l1;
l1 = l1.next;
} else {
cur.next = l2;
l2 = l2.next;
}
cur = cur.next;
}
cur.next = (l1 != null) ? l1 : l2;
return dummy.next;
}
}

C++ 解法二：

class Solution {

public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1) return l2;
if (!l2) return l1;
if (l1->val < l2->val) {
l1->next = mergeTwoLists(l1->next, l2);
return l1;
} else {
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}
}
};

Java 解法二：

public class Solution {

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
if (l1.val < l2.val) {
l1.next = mergeTwoLists(l1.next, l2);
return l1;
} else {
l2.next = mergeTwoLists(l1, l2.next);
return l2;
}
}
}

C++ 解法三：

class Solution {

public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1) return l2;
if (!l2) return l1;
ListNode *head = l1->val < l2->val ? l1 : l2;
ListNode *nonhead = l1->val < l2->val ? l2 : l1;
}
}; 

Java 解法三：

public class Solution {

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null) return l2;
if (l2 == null) return l1;
ListNode head = (l1.val < l2.val) ? l1 : l2;
ListNode nonhead = (l1.val < l2.val) ? l2 : l1;
}
}

C++ 解法四：

class Solution {

public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if (!l1 || (l2 && l1->val > l2->val)) swap(l1, l2);
if (l1) l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
}; 

Java 解法四：

public class Solution {

 public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l1 == null || (l2 != null && l1.val > l2.val)) {
ListNode t = l1; l1 = l2; l2 = t;
}
if (l1 != null) l1.next = mergeTwoLists(l1.next, l2);
return l1;
}
}

https://discuss.leetcode.com/topic/2513/a-recursive-solution

https://discuss.leetcode.com/topic/18709/3-lines-c-12ms-and-c-4ms/2

https://discuss.leetcode.com/topic/32953/java-recursive-solution-in-6-lines

，如需转载请自行联系原博主。

|
4月前
|

LeetCode刷题---21.合并两个有序链表（双指针）
LeetCode刷题---21.合并两个有序链表（双指针）
56 0
|
4月前
LeetCode | 21. 合并两个有序链表
LeetCode | 21. 合并两个有序链表
22 0
|
2月前
|
Java

26 2
|
3月前
|

【经典算法】LeetCode 21:合并两个有序链表Java/C/Python3实现含注释说明,Easy)
【经典算法】LeetCode 21:合并两个有序链表Java/C/Python3实现含注释说明,Easy)
28 1
|
3月前
|
SQL 算法 数据挖掘
LeetCode 二十一：合并两个有序链表 【python】
LeetCode 二十一：合并两个有序链表 【python】
45 0
|
4月前

30 0
|
4月前
|

【力扣刷题】回文链表、环形链表、合并两个有序链表
【力扣刷题】回文链表、环形链表、合并两个有序链表
32 0
|
4月前
|
C语言

35 1
|
4月前

32 0
|
4月前
leetcode：21. 合并两个有序链表
leetcode：21. 合并两个有序链表
19 0

DDNS