[LeetCode]21.Merge Two Sorted Lists,leetcode21.merge

來源:互聯網
上載者:User

[LeetCode]21.Merge Two Sorted Lists,leetcode21.merge
【題目】

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.

【分析】

無

【代碼】

/**********************************   日期:2015-01-06*   作者:SJF0115*   題目: 21.Merge Two Sorted Lists*   來源:https://oj.leetcode.com/problems/merge-two-sorted-lists/*   結果:AC*   來源:LeetCode*   部落格:**********************************/#include <iostream>using namespace std;struct ListNode{    int val;    ListNode *next;    ListNode(int x):val(x),next(NULL){}};class Solution {public:    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {        ListNode *head = new ListNode(-1);        ListNode *p1 = l1,*p2 = l2,*cur = head;        while(p1 != NULL && p2 != NULL){            // 取兩個鏈表頭的較小元素            if(p1->val < p2->val){                cur->next = p1;                p1 = p1->next;            }//if            else{                cur->next = p2;                p2 = p2->next;            }//else            cur = cur->next;        }//while        // 如果鏈表1沒有遍曆完        while(p1){            cur->next = p1;            p1 = p1->next;            cur = cur->next;        }//while        // 如果鏈表2沒有遍曆完        while(p2){            cur->next = p2;            p2 = p2->next;            cur = cur->next;        }//while        cur->next = NULL;        return head->next;    }};int main() {    Solution solution;    int A[] = {1,2,4,7,9};    int B[] = {3,5,8,10,11,12};    // 鏈表1    ListNode *head1 = new ListNode(A[0]);    ListNode *p1 = head1;    for(int i = 1;i < 5;i++){        ListNode *node = new ListNode(A[i]);        p1->next = node;        p1 = node;    }//for    // 鏈表2    ListNode *head2 = new ListNode(B[0]);    ListNode *p2 = head2;    for(int i = 1;i < 6;i++){        ListNode *node = new ListNode(B[i]);        p2->next = node;        p2 = node;    }//for    ListNode *head = solution.mergeTwoLists(head1,head2);    // 輸出    ListNode *p = head;    while(p){        cout<<p->val<<" ";        p = p->next;    }//while    cout<<endl;}



【代碼二】

遞迴實現

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(l2->next, l1);            return l2;        }    }};



【代碼三】

public:    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {        ListNode *head = new ListNode(-1);        for(ListNode *p = head;l1 != NULL || l2 != NULL;p = p->next){            int val1 = (l1 == NULL) ? INT_MAX : l1->val;            int val2 = (l2 == NULL) ? INT_MAX : l2->val;            if(val1 <= val2){                p->next = l1;                l1 = l1->next;            }//if            else{                p->next = l2;                l2 = l2->next;            }//else        }//for        return head->next;    }};



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.