【数据结构_链表_最小堆】 链表找环,链表找交点,合并有序链表
来源:互联网 发布:linux 查ip 编辑:程序博客网 时间:2024/06/05 20:45
Linked List Cycle
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool hasCycle(ListNode *head) { ListNode *p=head,*q,*f=NULL; while(p){//对链表反序,如果p回到起点,则有环 q=p->next; p->next=f; f=p;p=q; if(p==head) return true;//有环 } return false;//沒有环,p到了NULL }};
合并有序链表
用优先队列(小顶堆)
class Solution {public: struct cmp{ bool operator () ( const pair<int, int> &a, const pair<int, int > &b){ return a.first >= b.first; } }cc; ListNode *mergeKLists(vector<ListNode *> &lists) { priority_queue<pair<int, int>, vector<pair<int, int> >, cmp> que; for(int i=0; i<lists.size(); i++) if(lists[i]) que.push(pair<int, int>(lists[i]->val, i)); ListNode *head =NULL, *tail =NULL; while(!que.empty()){ pair<int, int> t = que.top(); que.pop(); int i = t.second; if(!head) head = lists[i]; else tail -> next = lists[i]; tail = lists[i]; lists[i] = lists[i]->next; if(lists[i]) que.push(pair<int, int>(lists[i]->val, i)); } if(tail) tail->next = NULL; return head; }};
0 0
- 【数据结构_链表_最小堆】 链表找环,链表找交点,合并有序链表
- 数据结构整理_有序链表合并
- 数据结构整理_有序顺序表合并
- 17_合并两个有序链表
- 数据结构_中国大学MOOC(慕课)——两个有序链表序列的合并问题
- 20170713_合并有序链表_u
- 树_最小堆
- 数据结构_链表_单向链表
- 数据结构_合并排序
- 数据结构_堆排序
- 【算法】合并k个有序的链表-基于最小堆的思想
- 剑指Offer_面试题17_合并两个有序链表
- 20170909_合并两个有序数组
- 1465 数据结构:有序链表的合并
- sdut.acm2012级《程序设计基础Ⅱ)》_链表 数据结构实验之链表四:有序链表的归并
- 数据结构_静态链表_哈夫曼
- 【数据结构_堆_1098】堆的判断
- 【链表】合并两个有序链表,合并后链表依旧有序
- 阿里巴巴北京实习 Week 1
- DFS【The song of Ice and Fire】
- 线程的两种不同实现方式
- 如何安装arm-linux-gcc
- cv::Mat到YUV420的转换
- 【数据结构_链表_最小堆】 链表找环,链表找交点,合并有序链表
- CAN总线的远程、错误帧的作用!
- Java优质博客记录
- java中的路径关联
- [LeetCode] Single Number II
- 黑马程序员 java学习笔记——正则表达式
- AJAX入门--- XMLHttpRequest对象的属性和方法
- HDU-2128-Tempter of the Bone II(BFS)
- JNI注册