当前位置:网站首页>剑指offer:合并两个排序的链表
剑指offer:合并两个排序的链表
2022-08-02 14:11:00 【超级码力奥】
我是真的傻逼。
https://www.acwing.com/solution/content/744/
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* merge(ListNode* l1, ListNode* l2) {
// 我在刚开始也想到弄一个虚的头节点来着!
ListNode* dummy = new ListNode(0);
ListNode* p = dummy;
ListNode* i = l1;
ListNode* j = l2;
while(i!=NULL && j!=NULL)
{
if(i->val <= j->val)
{
p->next = i;
i = i->next;
p = p->next;
}
else
{
p->next = j;
j = j->next;
p = p->next;
}
}
p -> next = (i != NULL ? i : j);
return dummy -> next;
}
};
看看y总的多简洁:
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */
class Solution {
public:
ListNode* merge(ListNode* l1, ListNode* l2) {
ListNode *dummy = new ListNode(0);
ListNode *cur = dummy;
while (l1 != NULL && l2 != NULL) {
if (l1 -> val < l2 -> val) {
cur -> next = l1;
l1 = l1 -> next;
}
else {
cur -> next = l2;
l2 = l2 -> next;
}
cur = cur -> next;
}
cur -> next = (l1 != NULL ? l1 : l2);
return dummy -> next;
}
};
作者:yxc
链接:https://www.acwing.com/solution/content/744/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- A clean start Windows 7?How to load only the basic service start Windows 7 system
- Introduction to C language function parameter passing mode
- Detailed introduction to the hierarchical method of binary tree creation
- Project: combing the database table
- 1. Development community homepage, register
- Exotic curiosity-a solution looking - bit operations
- How to add a one-key shutdown option to the right-click menu in Windows 11
- jest test, component test
- Yolov5 official code reading - prior to transmission
- Win10 computer can't read U disk?Don't recognize U disk how to solve?
猜你喜欢
动态规划理论篇
Open the door to electricity "Circuit" (3): Talk about different resistance and conductance
6.统一记录日志
Exotic curiosity-a solution looking - bit operations
mysql学习总结 & 索引
STM32LL library - USART interrupt to receive variable length information
Mysql lock
pygame draw arc
【STM32学习1】基础知识与概念明晰
Spark及相关生态组件安装配置——快速回忆
随机推荐
为vscode配置clangd
pygame draw arc
LeetCode 2353. 设计食物评分系统 维护哈希表+set
MATLAB绘图函数fplot详解
Exotic curiosity-a solution looking - bit operations
动态数组-vector
How to set the win10 taskbar does not merge icons
Use tencent cloud builds a personal blog
Open the door of power and electricity "Circuit" (2): Power Calculation and Judgment
How to reinstall Win7 system with U disk?How to reinstall win7 using u disk?
动态规划理论篇
3. User upload avatar
TCP三次握手、四次挥手
word方框怎么打勾?
LeetCode 2354. 优质数对的数目 二进制01表示和集合之间的转换
编译error D8021 :无效的数值参数“/Wextra” cl command line error d8021 invalid numeric argument ‘/wextra‘
MATLAB drawing command fimplicit detailed introduction to drawing implicit function graphics
Introduction to in-order traversal (non-recursive, recursive) after binary tree traversal
Detailed explanation of MATLAB drawing function fplot
C语言函数参数传递模式入门详解