劍指 offer 鏈表合併

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/
class Solution {
public:
    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
    {
        if (pHead1 ==NULL){
            return pHead2;
        }
        if (pHead2 ==NULL){
            return pHead1;
        }
        ListNode * result;
        if (pHead1->val < pHead2->val){
            result = pHead1;
            result->next =  Merge(pHead1->next,pHead2);
         
        }
        else{
            result = pHead2;
            result->next =  Merge(pHead1,pHead2->next);
        }
        return result;
        
    }
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章