题目描述
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.
Example:
Input: 1->2->4, 1->3->4
Output: 1->1->2->3->4->4
源码
/**
* 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)
{
int min=0, temp=0;
ListNode *p=l1;
ListNode *q=l1;
if(!l1)
return l2;
if(!l2)
return l1;
while(p->next)//令指针指向末尾
p=p->next;
p->next=l2;//连接链表
p=l1;
for(p=l1;p;p=p->next)//冒泡排序
{
for(q=p->next;q;q=q->next)
{
if(p->val>q->val)
{
temp=q->val;
q->val=p->val;
p->val=temp;
}
}
}
return l1;
}
};
提交
执行用时 :8 ms, 在所有 cpp 提交中击败了95.89%的用户;
内存消耗 :9 MB, 在所有 cpp 提交中击败了76.13%的用户。