【leetcode c++】21 Merge Two Sorted Lists
来源:互联网 发布:黑暗骑士 知乎 编辑:程序博客网 时间:2024/04/25 14:58
Merge two sorted linked lists and return itas a new list. The new list should be made by splicing together the nodes ofthe first two lists.
融合两个有序的链表,返回这个新链表。
其实呢,这也是数据结构里面的链表的题(类似多项式的加法),不过稍微比当年做的要难一点点点,因为这里的输入没有头结点。
这次还是在leetcode网页上面写的,但是还是经历了不少坎坷,最后还是要在纸上演习了一遍指针操作最后才做出来(脑补力不足→ →)。
我们先把【融合】放到一边,所谓融合,在文字的概念上,应该是两者相互融合,那么这两条链表在主动性上是等价的,这样考虑解决方案有点迷。我们换一个说法,把一个链表插入到领一个链表里,那么就有插入与被插入的区别了。就跟多项式的的四则运算一样,每个运算都有【X数】和【被X数】的区分。
那么我们就分别考虑插入链表和被插入链表就好了。
我假设【插入链表】叫inserting,【被插入链表】叫inserted。
插入链表是肯定能插入进去的,我们只需要定位就OK了。
我们按‘对’扫描被插入链表,按‘个’扫描插入链表。如果插入的节点的大小刚好处于这对被插入节点之间,我们就做插入操作。做题的过程发现,是有相同节点的情况的,需要注意。
扫描到最后一对完毕之后,如果插入链表还有节点,那么剩下的节点肯定都比被插入节点的值还要大了,只要全部接在被插入链表之后就可以了。
主要还是在插入的操作出了点问题,最后还是在纸上进行演习才搞定。
图示:
*********************************************************************************************
*********************************************************************************************
*********************************************************************************************
*********************************************************************************************
*********************************************************************************************
*********************************************************************************************
*********************************************************************************************
inserted如果扫描完毕,inserting还有剩余,直接把剩余的inserting接到inserted后面就好了。
最后别忘了,插入操作做完之后,指针已经来到链表尾了,所以还要在一开始声明一个指向被插入链表的指针,并返回之。
Leetcode的AcceptedSolutions Runtime Distribution(15-06-07)
源码:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* resHead; ListNode* inserting; ListNode* inserted; ListNode* temp; if (!l1) return l2; if (!l2) return l1; if(l1->val < l2->val) { resHead = l1; inserted = l1; inserting = l2; } else { resHead = l2; inserted = l2; inserting = l1; } while(inserted->next && inserting) { if(inserted->val == inserting->val || ((inserted->val < inserting->val) && (inserted->next->val > inserting->val))) { temp = inserted->next; inserted->next = inserting; inserting = inserting->next; inserted->next->next = temp; inserted = inserted->next; } else inserted = inserted->next; } if(inserting) inserted->next = inserting; return resHead;}
- 【leetcode c++】21 Merge Two Sorted Lists
- 【LeetCode-21】 Merge Two Sorted Lists(C++)
- LeetCode 21 Merge Two Sorted Lists (C,C++,Java,Python)
- [C++]LeetCode: 13 Merge Two Sorted Lists
- Leetcode 21. Merge Two Sorted Lists(C++)
- 【C++】【LeetCode】21. Merge Two Sorted Lists
- Leetcode c语言- Merge Two Sorted Lists
- [C语言][LeetCode][21]Merge Two Sorted Lists
- LeetCode-21-Merge Two Sorted Lists(C语言实现)
- LeetCode(21)Merge Two Sorted Lists
- [LeetCode 21] Merge Two Sorted Lists Solution
- [leetcode 21] Merge Two Sorted Lists
- LeetCode 21:Merge Two Sorted Lists
- LeetCode | #21 Merge Two Sorted Lists
- [Leetcode] 21 - Merge Two Sorted Lists
- leetcode.21------------Merge Two Sorted Lists
- [leetcode]21 Merge Two Sorted Lists
- Leetcode NO.21 Merge Two Sorted Lists
- 唐巧的iOS技术博客好文列表
- Theano2.1.13-基础知识之PyCUDA、CUDAMat、Gnumpy的兼容
- OOAD练习题
- 数据库事物隔离机制
- Mybaits的映射器配置详解
- 【leetcode c++】21 Merge Two Sorted Lists
- 迷你mysql操作类
- Boush
- iOS 多语言支持
- Extjs4之 formpanel使用
- prototype与[[prototype]]
- 我的Java开发学习之旅------>Java经典排序算法之插入排序
- Ubuntu,mac查看端口命令
- POJ 3660 Cow Contest (Floyd求传递闭包)