Linked List Cycle II
来源:互联网 发布:迅雷 for mac 老版本 编辑:程序博客网 时间:2024/06/02 06:25
Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
Follow up:
Can you solve it without using extra space?
#include<stdio.h>#include<stdlib.h>typedef struct ListNode { int val; struct ListNode *next; }*ListNode;ListNode* detectCycle(ListNode *head) { ListNode *p1,*p2,*p3; int n,m,flag; if(head==NULL||head->next==NULL) return NULL; if(head->next==head) return head; for(p1=p2=head;p1!=NULL&&p1->next!=NULL;){ p1=p1->next->next; p2=p2->next; if(p1==p2) break; } if(p1==NULL || p1->next==NULL) return NULL; n=1; while(p1->next!=p2){ p1=p1->next; n++; } for(p1=head; p1!=p2; p1=p1->next){ for(p3=p1, m=0, flag=0; p3->next!=p1 && m<=n; p3=p3->next){ if(p3==p2) flag=1; if(flag==1) m++; } if(p3->next==p1) return p1; } return p2;}
0 0
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- Linked List Cycle II
- 数据库使用
- 使用 Twitter Storm 处理实时的大数据
- SlidingPaneLayout实现类似新版QQ主页滑动缩放的功能。
- mysql备份还原-centos6.5下安装xtrabackup2.2.3(二进制,源码包,rpm包安装方式汇总)
- 强引用、软引用、弱引用、虚引用
- Linked List Cycle II
- ARM PL330 DMA控制器开发(1)
- IOS开发---C语言-⑫指针与数组、字符串
- zuiqingchun
- Linux常用命令(三十一) - grep
- BZOJ 3626 LNOI 2014 LCA 树链剖分
- springmvc在ajax请求出现406
- 3. Dubbo原理解析-Dubbo内核实现之动态编译
- R: 矩阵运算及常用函数 III - *apply Part I