Problem 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 two lists.
题目大意:
合并两个已排好序的单向链表
Solutions:
非常简单的链表基本操作,实现即可。
Code in C++:
/**
* 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) return l2;
if(!l2) return l1;
ListNode* l3;
if(l1->val<l2->val)
{
l3=l1;
l3->next=mergeTwoLists(l1->next,l2);
}else{
l3=l2;
l3->next=mergeTwoLists(l1,l2->next);
}
return l3;
}
};
近期评论