将两个排序链表合并为一个新的排序链表
作者:
杰米 | 来源:发表于
2016-08-12 00:12 被阅读15次/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
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
if (l1 == NULL) {
return l2;
}
if(l2 == NULL) {
return l1;
}
ListNode *maxHead;
if (l1->val < l2->val) {
maxHead = l1;
maxHead->next = mergeTwoLists(l1->next,l2);
} else {
maxHead = l2;
maxHead->next = mergeTwoLists(l1,l2->next);
}
return maxHead;
}
};
本文标题:将两个排序链表合并为一个新的排序链表
本文链接:https://www.haomeiwen.com/subject/rqbssttx.html
网友评论