LintCode-劍指Offer-(165)合併兩個排序鏈表

class Solution {
public:
    /**
    * @param ListNode l1 is the head of the linked list
    * @param ListNode l2 is the head of the linked list
    * @return: ListNode head of linked list
    */
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        // write your code here
        ListNode* l3 = new ListNode(0);
        ListNode* l4=l3;
        ListNode* l5=l4;
        while ( l1 != NULL&&l2 != NULL ){
            if ( l1->val < l2->val ){
                l3->next = l1;
                l1 = l1->next;
                l3 = l3->next;
            }
            else {
                l3->next = l2;
                l2 = l2->next;
                l3 = l3->next;
            }
        }
        if ( l1 == NULL ){
            l3->next = l2;
        }else
            l3->next = l1;
        delete l5;
        return l4->next;
    }

};
發佈了53 篇原創文章 · 獲贊 3 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章