LeetCode--delete/merge sorted list
2015-08-21 14:13
183 查看
Problem I : Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.删除所有有重复的节点。
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
Tags: Linked List
[code]#include <iostream> using namespace std; /*Problem:Remove Duplicates from Sorted List II Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. */ struct ListNode{ int val; ListNode *next; ListNode(int x):val(x),next(nullptr){} }; ListNode* deleteDuplicates(ListNode* head) { if(!head||head->next==nullptr)return head; ListNode dump(-1); ListNode *p_new=&dump; ListNode *p=head; bool flag=true; while(p){ if(p->next&&p->val==p->next->val){ ListNode *q=p->next; p->next=p->next->next; flag=false; delete q; } else { if(flag){ p_new->next=p; p=p->next; p_new=p_new->next; p_new->next=nullptr; } else { flag=true; ListNode *q=p; p=p->next; delete q; } } } return dump.next; } int main() { ListNode l1(1),l2(3),l3(3),l4(3),l5(5); l1.next=&l2;l2.next=&l3;l3.next=&l4;l4.next=&l5; ListNode *head=deleteDuplicates(&l1); while(head){cout<<head->val<<" ";head=head->next;} cout << "Hello world!" << endl; return 0; }
Problem II: Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.问题是合并k个排序的链表,合并后的链表保持有序。
分析:第一次使用线性遍历 方式实现合并,结果出现超时测试用例;也算有点纪念意义,代码如下:
[code] ListNode* mergeKLists(vector<ListNode*>& lists) { ListNode dump(-1); int n=lists.size(); if(n==0)return dump.next; ListNode *p=&dump;p->next=lists[0]; for(int i=1;i<n;i++){ ListNode *head=lists[i]; ListNode *q=p->next; while(q&&head){ if(q->val<=head->val) { p->next=q; p=p->next; q=q->next; } else{ p->next=head; p=p->next; head=head->next; } } if(q){p->next=q;} else if(head){p->next=head;} p=&dump; } return dump.next; }
分析:使用归并思想。两两合并->最终合为一个链表。我没有看出来这个思路有什么时间上的提升,应该是平均时间复杂度有所提升吧。但看讨论里,发现这个方法的代码能通过测试。代码如下:
[code]#include <iostream> #include<vector> using namespace std; /*Problem:Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. */ struct ListNode{ int val; ListNode *next; ListNode(int x):val(x),next(nullptr){} }; ListNode* mergelist(ListNode* l1,ListNode* l2){ if(!l1)return l2; if(!l2)return l1; if(l1->val<=l2->val){ l1->next=mergelist(l1->next,l2); return l1; } else{ l2->next=mergelist(l1,l2->next); return l2; } } ListNode* mergeKLists(vector<ListNode*>& lists) { //ListNode dump(-1); int n=lists.size(); if(n==0)return nullptr; while(n>1){ for(int i=0;i<n/2;i++){ lists[i]=mergelist(lists[i],lists[n-1-i]); } n=(n+1)/2; } return lists.front(); } int main() { ListNode L1_1(1),L1_2(4),L1_3(5),L1_4(9),L1_5(11),L1_6(12); L1_1.next=&L1_2;L1_2.next=&L1_3;L1_3.next=&L1_4;L1_4.next=&L1_5;L1_5.next=&L1_6; ListNode L2_1(2),L2_2(3),L2_3(4),L2_4(6),L2_5(7),L2_6(10); L2_1.next=&L2_2;L2_2.next=&L2_3;L2_3.next=&L2_4;L2_4.next=&L2_5;L2_5.next=&L2_6; ListNode L3_1(0),L3_2(2); L3_1.next=&L3_2; vector<ListNode*> lists={&L1_1,&L2_1,&L3_1}; ListNode* res=mergeKLists(lists); while(res){cout<<res->val<<" ";res=res->next;} cout << "Hello world!" << endl; return 0; }
相关文章推荐
- Spring Dependency Injection (DI)
- ORA-01034: ORACLE not available
- 今目标文荣演讲:主线+连接一切的力量
- Java 堆排序
- HBase表设计——Column Family列族的设计数量不宜过多
- Hadoop文件系统元数据fsimage和编辑日志edits
- IOS常用第三方库
- 富有动感的 Sheet(选择器)
- android 用fragment实现简单菜单点击功能
- 欢迎使用CSDN-markdown编辑器
- MySQL不支持的特性
- Parallels Desktop 11 for Mac 发布啦!
- 使用jquery easyui 对表格中的时间格式化
- mybatis半自动生成Bean和简单查询SQL
- 模仿Airbnb的悬浮搜索框动画
- [LeetCode 173] Binary Search Tree Iterator
- 洛谷1098 字符串的展开 解题报告
- poj 1129 Channel Allocation(DFS减枝)
- 第一份软件测试实习(1)
- linux文件压缩命令