一、 问题描述
Leecode第二十一题,题目为:
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
问题理解为:
合并两个已排序的链表,并将其作为一个新列表返回。新列表应该通过将前两个列表的节点拼接在一起来创建。
例:
输入:1 - > 2 - > 4,1 - > 3 - > 4
输出:1 - > 1 - > 2 - > 3 - > 4 - > 4
二、算法思路
1、
2、
三、实现代码
struct ListNode *mergeTwoLists(struct ListNode *l1, struct ListNode *l2) {
struct ListNode res;
struct ListNode *tail = &res;
while (l1 && l2) {
if (l1->val < l2->val) {
tail->next = l1;
l1 = l1->next;
}
else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
tail->next = l1 ? l1 : l2;
return res.next;
}
转自:https://blog.csdn.net/junlon2006/article/details/53924809
博客围绕LeetCode第二十一题展开,题目要求合并两个已排序的链表并返回新列表,新列表由前两个列表节点拼接而成,还给出了示例输入输出,后续将阐述算法思路并给出实现代码。

被折叠的 条评论
为什么被折叠?



