leetcode挨个儿刷150105(1):Merge Two Sorted Lists
来源:互联网 发布:格灵深瞳 知乎 编辑:程序博客网 时间:2024/06/05 00:48
随着找工作的日期一天天的逼近,是时候在科研之余写一写面试题中可能出现的代码了,虽然说经常看开源框架的代码,在面对这些简短的题目时还是感觉有点儿难度,好在题目不多,索性从最简单的做起来呗,加油!争取每周做五个,每周末来个总结吧。
Long Way To Go!
No1: https://oj.leetcode.com/submissions/detail/17943308/
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.
<span style="font-size:18px;"> /* Definition for singly-linked list.*/ #include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; </span>
解决方案:
<span style="font-size:18px;">class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if( (l1 == NULL) && (l2 == NULL) ) return NULL; //写完代码反省的时候,发现冗余了,可以不要。 else if(l1 == NULL) return l2; else if(l2 == NULL) return l1; ListNode *p1; ListNode *p2; ListNode *l3=(l1->val < l2->val ? (p1=l1->next,p2=l2,l1) :(p2=l2->next,p1=l1,l2)); ListNode *p3=l3; while( (p1 != NULL) && ( p2 != NULL )) { if(p1->val < p2->val) { p3->next = p1; p3 = p3->next; p1 = p1->next; } else { p3->next = p2; p3 = p3->next; p2 = p2->next; } } if (p1 == NULL) { p3->next = p2; } else if(p2 == NULL) { p3->next = p1; } return l3; } };</span>
程序的测试部分:
<span style="font-size:18px;">int main(){ListNode * l1= new ListNode(2);ListNode * l2 = new ListNode(1);ListNode *p=l1;ListNode *q=l2;for(int i = 1; i <= 0; i++){ListNode *newnode=new ListNode(i);p->next=newnode;p=newnode;cout<<p->val<<endl;}for(int i = 2; i <= 1; i += 2){ListNode *newnode=new ListNode(i);q->next=newnode;q=newnode;cout<<q->val<<endl;}Solution s;ListNode *l3 = s.mergeTwoLists(l1,l2);for(;l3!=NULL;l3=l3->next)cout<<l3->val;return 0;}</span>
分析代码,其时间复杂度为O(min(m,n)),其中,m和n为两个链表的长度。
提交后,看到运行时间属于较短的,可以接受。
但是总的说来,自己的代码写的很冗长,且不漂亮,看到《Leetcode题解》书中的范例程序如下:
<span style="font-size:18px;">//LeetCode, Merge Two Sorted Lists// 时间复杂度 O(min(m,n)) ,空间复杂度 O(1)class Solution { public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { if (l1 == nullptr) return l2; if (l2 == nullptr) return l1; ListNode dummy(-1); ListNode *p = &dummy; for (; l1 != nullptr && l2 != nullptr; p = p->next) { if (l1->val > l2->val) { p->next = l2; l2 = l2->next; } else { p->next = l1; l1 = l1->next; } } p->next = l1 != nullptr ? l1 : l2; return dummy.next; }};</span>
代码比较精简,但是多用了一个存储空间,可对dummy进行改进,使p一开始就指向l1或者l2的头。
0 0
- leetcode挨个儿刷150105(1):Merge Two Sorted Lists
- LeetCode: Merge Two Sorted Lists
- [Leetcode] Merge Two Sorted Lists
- Leetcode: Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- [Leetcode] Merge two sorted lists
- leetcode Merge Two Sorted Lists
- [LeetCode]Merge Two Sorted Lists
- [leetcode]Merge Two Sorted Lists
- LeetCode-Merge Two Sorted Lists
- [leetcode] Merge Two Sorted Lists
- [LeetCode] Merge Two Sorted Lists
- LeetCode - Merge Two Sorted Lists
- LeetCode:Merge Two Sorted Lists
- LeetCode | Merge Two Sorted Lists
- 【leetcode】Merge Two Sorted Lists
- Leetcode: Merge Two Sorted Lists
- 【LeetCode】Merge Two Sorted Lists
- 判断两个增序数组是否有重复(度娘)
- 修改win32平台下窗口大小,及屏幕适配
- 32位小写MD5加密js和sha256哈希js
- ReactiveCocoa--RACDelegateProxy
- 三维引擎设计专题--文字的渲染
- leetcode挨个儿刷150105(1):Merge Two Sorted Lists
- bzoj1022: [SHOI2008]小约翰的游戏John 博弈
- RESTful实践总结
- C++ primer 单词转换(使用map对象)
- Ruby on rails 入门胡来版 (一) 准备工作
- 素数表
- Understanding HBase and BigTable
- Android开发之自定义View专题(四):自定义ViewGroup
- java.lang.NoClassDefFoundError: org/apache/jsp/WEB_002dINF/views/common/showPageBespeak_jsp