一、思路
二、源码

一、思路
创建一个新链表
两个链表比较,小于等于取下来尾插
循环结束条件为任意一个链表为空
最后将之剩下的链表直接尾插
二、源码
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2){
if(list1 == NULL)
return list2;
if(list2 == NULL)
return list1;
struct ListNode *head = NULL,*tail = NULL;
while(list1 && list2)
{
if(list1->val <= list2->val)
{
if(head == NULL)
{
head = tail = list1;
}
else
{
tail->next = list1;
tail = tail->next;
}
list1 = list1->next;
}
else
{
if(head == NULL)
{
head = tail = list2;
}
else
{
tail->next = list2;
tail = tail->next;
}
list2 = list2->next;
}
}
if(list1)
tail->next = list1;
if(list2)
tail->next = list2;
return head;
} 猜你喜欢
- 3月前18种好运来临的征兆
- 3月前梦见小孩丢了预示吉凶解析
- 3月前女人梦见吃饭的心理学解析
- 3月前梦见枕头的寓意解析
- 3月前梦见考试预示什么心理暗示
- 3月前梦中被巨型蜘蛛追逐的深层解析
- 3月前女性梦境解析:细蛇的象征意义
- 3月前梦见大量水的深层含义解析
- 3月前梦中初尝飞翔的生涩体验
- 3月前梦中狂风暴雨的深层心理解析
网友评论
- 搜索
- 最新文章
- 热门文章
