1. 程式人生 > >21. Merge Two Sorted Lists 一道基礎題的兩種精煉解法

21. Merge Two Sorted Lists 一道基礎題的兩種精煉解法

方法一:遞迴

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        if(l1 == NULL) return l2;
        if(l2 == NULL) return l1;
        
        if(l1->val > l2->val){
            l2->next = mergeTwoLists(l1, l2->next);
            return l2;
        }else{
            l1->next = mergeTwoLists(l1->next, l2);
            return l1;
        }
    }
};

法二:遞迴的精煉程式碼:
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode dummy(1);
        ListNode* tail = &dummy;// 妙, 設定指向尾的指標, 並且對dummy取地址,不用new了,函式結束後自動釋放
        while(l1 && l2){
            if(l1->val > l2->val){
                tail->next = l2;
                l2 = l2->next;
            }else{
                tail->next = l1;
                l1 = l1->next;
            }
            tail = tail->next;
        }
        //tail->next = l1 ? l1 : l2;
        l1 == NULL? tail->next = l2: tail->next = l1;
        return dummy.next;
    }
};


相關推薦

21. Merge Two Sorted Lists 一道基礎精煉解法

方法一:遞迴 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNo

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

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

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

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

#21 Merge Two Sorted Lists

1 /** 2 * Definition for singly-linked list. 3 * public class ListNode { 4 * int val; 5 * ListNode next; 6 * ListNode(int x) {

[leetcode] 21. Merge Two Sorted Lists (Easy)

合併連結串列 Runtime: 4 ms, faster than 100.00% of C++ online submissions for Merge Two Sorted Lists. class Solution { public: ListNode *mergeTwoList

[Leetcode]21. Merge Two Sorted Lists

合併兩個排序好的列表,遞迴演算法pass /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x)

【LeetCode】21. Merge Two Sorted Lists - Java實現

文章目錄 1. 題目描述: 2. 思路分析: 3. Java程式碼: 1. 題目描述: Merge two sorted linked lists and return it as a new list. The new list

21 Merge Two Sorted Lists

水題 class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (l1 == NULL&&l2 == NULL)return l1; ListNode *f

C# 寫 LeetCode easy #21 Merge Two Sorted Lists

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 fi

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 fir

Leetcode_Linked_List --21. Merge Two Sorted Lists [easy]

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&Python] Problem 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: In