Merge Two Sorted Lists
来源:互联网 发布:百度学术 知乎 编辑:程序博客网 时间:2024/06/07 02:05
一开始用循环写 time limit exceeded了
也不知道为啥
原来的
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { if(!l1){ return l2; } if(!l2){ return l1; } struct ListNode* result; result=(struct ListNode*)malloc(sizeof(struct ListNode)); struct ListNode* temp=result; while(l1&&l2){ if(l1->val>l2->val){ result->val=l2->val; l2=l2->next; }else{ result->val=l1->val; l1=l1->next; } result->next=(struct ListNode*)malloc(sizeof(struct ListNode)); result=result->next; } while(l1){ result->val=l1->val; result->next=(struct ListNode*)malloc(sizeof(struct ListNode)); result=result->next; l1=l1->next; } while(l2){ result->val=l2->val; result->next=(struct ListNode*)malloc(sizeof(struct ListNode)); result=result->next; l2=l2->next; } return temp;}
后来看网上 用递归写
struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2){ if(!l1){ return l2; } if(!l2){ return l1; } if(l1->val>l2->val){ l2->next=mergeTwoLists(l1,l2->next); return l2; }else{ l1->next=mergeTwoLists(l1->next,l2); return l1; }}
最基本情况就是一个空的时候
就返回另外一个
然后就从头比较如果一个值大于另外一个就 接上去
道理都懂 然后自己目前咋写不出来
可能多写写就行了吧?
0 0
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Merge Two Sorted Lists
- Python中原始字符串操作符 r R
- Amber大神的《最小割模型在OI中的应用》膜后感
- 常用手势
- 13. 在O(1)时间删除链表结点
- Linux压缩打包命令汇总
- Merge Two Sorted Lists
- 【Android资料】Android软键盘显示模式总结
- eclipse 配置SVN步骤
- 剑指Offer面试题31(java版):连续子数组的最大和
- 记忆化dp hdu1978
- 主键引起的1062错误主键重复Hibernate
- BestCoder Round #50 (div.2) 1001
- hdu 5215 判断奇偶环
- java多文本框使用右键弹出菜单复制粘贴剪切功能实现