1. 程式人生 > >LeetCode21 Merge Two Sorted Lists

LeetCode21 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)

分析:

連結串列題,注意細節即可。

連結串列merge和陣列merge的不同在於連結串列不用拷一份出來,倒騰一下指標就可以啦。

注意事項有:

1. dummy node用於輸出,head(或者換個名字)用於遍歷;

2. l1,l2是不是為空,一個結束遍歷之後記得把另一個剩下的加上去。

程式碼:

 1 class Solution {
 2 public:
 3     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
 4         if (l1 == nullptr) {
 5             return l2;
 6         }
 7         if (l2 == nullptr) {
 8             return l1;
 9         }
10         ListNode dummy(0);
11         ListNode* head = &dummy;
12 while (l1 != nullptr && l2 != nullptr) { 13 if (l1 -> val < l2 -> val) { 14 head -> next = l1; 15 head = head -> next; 16 l1 = l1 -> next; 17 } 18 else { 19 head -> next = l2;
20 head = head -> next; 21 l2 = l2 -> next; 22 } 23 } 24 if (l1 != nullptr) { 25 head -> next = l1; 26 } 27 if (l2 != nullptr) { 28 head -> next = l2; 29 } 30 return dummy.next; 31 32 } 33 };

優化一下:

head = head -> next是不論if 還是else都要做的,拿出來寫;

按本題的寫法和ListNode的定義,其實開始不用判斷l1,l2是否為空。(但一般來講還是寫上為好...)

程式碼:

 1 class Solution {
 2 public:
 3     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
 4         ListNode dummy(0);
 5         ListNode* head = &dummy;
 6         while (l1 != nullptr && l2 != nullptr) {
 7             if (l1 -> val < l2 -> val) {
 8                 head -> next = l1;
 9                 l1 = l1 -> next;
10             }
11             else {
12                 head -> next = l2;
13                 l2 = l2 -> next;
14             }
15             head = head -> next;
16         }
17         if (l1 != nullptr) {
18             head -> next = l1;
19         }
20         if (l2 != nullptr) {
21             head -> next = l2;
22         }
23         return dummy.next;
24     }
25 };

今天七夕,算是半個假期,狀態不是很好,水一道連結串列題練練手啦。明天開始重新步入正軌按照題號刷啦!!!

相關推薦

LeetCode21. 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. Exampl

leetcode21. Merge Two Sorted Lists(連結串列)

題目 歸併連結串列, 要注意一下連結串列的初始化問題, 尤其對於指標要謹慎處理, 很容易空指標異常 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListN

LeetCode21 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)

Leetcode 21. Merge Two Sorted Lists

oge span solution clas next ini 遞歸解法 else get Merge two sorted linked lists and return it as a new list. The new list should be made by s

leetcode 21 -- Merge Two Sorted Lists

tro leet lan 常見 ber 題目 tty Language node Merge Two Sorted Lists 題目: Merge two sorted linked lists and return it as a ne

LeetCode——Merge Two Sorted Lists

sort -h ould des family tco 返回 https style Merge two sorted linked lists and return it as a new list. The new list should be made by

leetcode - Merge Two Sorted Lists

cin height mod sorted col truct pos data- first Merge two sorted linked lists and return it as a new list. The new list should be mad

[LeetCode] Merge Two Sorted Lists

合並 linked new ace oge tco div nullptr etc Merge two sorted linked lists and return it as a new list. The new list should be made by splic

21. Merge Two Sorted Lists

oge get clear des eth spl pull cnblogs val 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list

[LintCode] Merge Two Sorted Lists

blog lintcode code sts amp oge pro lint link Merge two sorted (ascending) linked lists and return it as a new sorted list. The new sorte

21. Merge Two Sorted Lists【easy】

wol int while min st2 遞歸 merge listnode rst 21. Merge Two Sorted Lists【easy】 Merge two sorted linked lists and return it as a new list.

Leetcode 21. Merge Two Sorted Lists(easy)

tco fin public def div wol lists lis else Merge two sorted linked lists and return it as a new list. The new list should be made by spli

LeetCode.21 - Merge Two Sorted Lists

算法 tput out oge 其中 png 就會 std info Merge two sorted linked lists and return it as a new list. The new list should be made by splicing tog

165. Merge Two Sorted Lists【LintCode by java】

-i get In param des AD end itl spl Description Merge two sorted (ascending) linked lists and return it as a new sorted list. The new

Merge Two Sorted Lists (LL)

lis style head listnode get sort urn nbsp sts 1 //10 ms 2 class Solution { 3 public ListNode mergeTwoLists(ListNode l1, ListNode

merge-two-sorted-lists合並鏈表

nod merge div 鏈表 {} should ted sorted bsp Merge two sorted linked lists and return it as a new list. The new list should be made by splic

leetcode#21 Merge Two Sorted Lists

solution ould pub eth linked public and urn ini Merge two sorted linked lists and return it as a new list. The new list should be made by

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

leetcode:(21) Merge Two Sorted Lists(java)

package LeetCode_LinkedList; /** * 題目: * Merge two sorted linked lists and return it as a new list. * The new list should be made by spl

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. Exampl