Q2.6 Find the Beginning of the loop
来源:互联网 发布:centos配置sql2012环境 编辑:程序博客网 时间:2024/05/14 14:34
Q:Given a circular linked list, implement an algorithm which returnsnode at the beginning of the loop.
A:
思路1:可以用哈希表记录每一个节点,遍历链表,通过哈希表记录判断,如果当前节点已经被访问过,那么有环。如果遍历结束,那么没有loop
思路2:
1、双指针查看是否有环。快指针速度为2, 慢指针速度为1
2、如果有环,则再利用双指针p = head, q = 快慢指针交点; p, q速度都是1, 相等的时候就是loop的起点
#include <iostream>using namespace std;struct ListNode { int val;ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode *init(int a[], int n) { ListNode *head = NULL; ListNode *p = NULL; for (int i = 0; i < n; i++) { ListNode *cur = new ListNode(a[i]); if (i == 0) { head = cur; p = cur; } p->next = cur; p = cur; } return head;}ListNode *detectCycle(ListNode *head) { if (!head) { return NULL; } ListNode *slow = head; ListNode *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) { ListNode *slow2 = head; while (slow2 != slow) { slow = slow->next; slow2 = slow2->next; } return slow2; } } return NULL;}void printList(ListNode *head) {ListNode *p = head;for( ; p; p = p->next) {cout<<p->val<<" ";}cout<<endl;}int main() {int a[10] = {2,4,3};int b[10] = {5,6,4};ListNode *head1 = init(a, 3);ListNode *head2 = init(b, 3);printList(head1);printList(head2);ListNode *head = addTwoNumbers(head1, head2);printList(head);return 0;}
0 0
- Q2.6 Find the Beginning of the loop
- the beginning of the loop
- Ch2-5: find the beginning of loop in a circular linked list---two solutions
- 【13】Find the start of loop
- THE BEGINNING OF CENTOS
- Beginning of the blog
- The day of beginning
- The Beginning of me
- The Beginning Of All
- Q2.2 Find the kth to last element of a singly linked list
- the beginning of a year
- The beginning of my blog.
- the beginning of learning struts
- The beginning of My blog
- The Beginning use of Polymorphism
- The Beginning of CRC Project
- The beginning of my blog
- The Beginning
- 如何使用setObject为PreparedStatement对象传入动态参数?
- 涉及帧间隙的CSMA/CD的侦听多久该发送
- 回调函数
- [翻译]十条有用的 Go 技术(转)
- python Loging 入门
- Q2.6 Find the Beginning of the loop
- 中文汉字存入char数组
- 《寒江独钓》内核学习笔记
- sgu-221 Big Bishops
- 阻塞队列BlockingQueue
- vim tips小结
- Head First PHP&MySQL学习笔记(四)
- VS2012 win32控制台调试总结
- 2D的水效果