C++實(shí)現(xiàn)LeetCode(21.混合插入有序鏈表)
[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.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
這道混合插入有序鏈表和我之前那篇混合插入有序數(shù)組非常的相似 Merge Sorted Array,僅僅是數(shù)據(jù)結(jié)構(gòu)由數(shù)組換成了鏈表而已,代碼寫起來反而更簡潔。具體思想就是新建一個(gè)鏈表,然后比較兩個(gè)鏈表中的元素值,把較小的那個(gè)鏈到新鏈表中,由于兩個(gè)輸入鏈表的長度可能不同,所以最終會有一個(gè)鏈表先完成插入所有元素,則直接另一個(gè)未完成的鏈表直接鏈入新鏈表的末尾。代碼如下:
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;
}
}
下面我們來看遞歸的寫法,當(dāng)某個(gè)鏈表為空了,就返回另一個(gè)。然后核心還是比較當(dāng)前兩個(gè)節(jié)點(diǎn)值大小,如果 l1 的小,那么對于 l1 的下一個(gè)節(jié)點(diǎn)和 l2 調(diào)用遞歸函數(shù),將返回值賦值給 l1.next,然后返回 l1;否則就對于 l2 的下一個(gè)節(jié)點(diǎn)和 l1 調(diào)用遞歸函數(shù),將返回值賦值給 l2.next,然后返回 l2,參見代碼如下:
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;
}
}
}
下面這種遞歸的寫法去掉了 if 從句,看起來更加簡潔一些,但是思路并沒有什么不同:
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;
head->next = mergeTwoLists(head->next, nonhead);
return head;
}
};
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;
head.next = mergeTwoLists(head.next, nonhead);
return head;
}
}
我們還可以三行搞定,簡直喪心病狂有木有!
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;
}
}
到此這篇關(guān)于C++實(shí)現(xiàn)LeetCode(21.混合插入有序鏈表)的文章就介紹到這了,更多相關(guān)C++實(shí)現(xiàn)混合插入有序鏈表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言中魔性的float浮點(diǎn)數(shù)精度問題
這篇文章主要介紹了魔性的float浮點(diǎn)數(shù)精度問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Qt圖形圖像開發(fā)之QT滾動區(qū)控件(滾動條)QScrollArea的詳細(xì)方法用法圖解與實(shí)例
這篇文章主要介紹了Qt圖形圖像開發(fā),QT滾動區(qū)控件(滾動條)QScrollArea的詳細(xì)方法用法圖解與實(shí)例,需要的朋友可以參考下2020-03-03
基于Matlab實(shí)現(xiàn)繪制3D足球的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用Matlab實(shí)現(xiàn)繪制3D足球,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Matlab有一定幫助,需要的可以參考一下2022-11-11
通過C++程序示例理解設(shè)計(jì)模式中的外觀模式
這篇文章主要介紹了通過設(shè)計(jì)模式中的外觀模式及相關(guān)的C++程序示例,外觀模式在高層提供了一個(gè)統(tǒng)一的接口實(shí)現(xiàn)一定程度上的解耦,需要的朋友可以參考下2016-03-03
C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(109.將有序鏈表轉(zhuǎn)為二叉搜索樹),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07

