Linked List Cycle II
来源:互联网 发布:金棕榈软件 编辑:程序博客网 时间:2024/05/18 03:43
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull
.
Follow up:
Can you solve it without using extra space?
解题思路:http://xingxjhui.blog.163.com/blog/static/2155451642014013154023/
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *detectCycle(ListNode *head) { if(!head) return NULL;//空结点 ListNode *p=head,*q=head; while(p && q){ p=p->next; if(q->next==NULL) return NULL; q=q->next->next; if(p==q) break; } if(p==NULL || q==NULL ){ return NULL; } p=head; while(p!=q){ p=p->next; q=q->next; } return p; }};
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
- android小程序之打电话、发短信
- 我的笔记
- ARM------Keil C51 与 ARM 并存方法
- 线程 thread的各种方法 join方法会抛异常 sleep方法会抛异常
- drbddisk
- Linked List Cycle II
- 文献管理软件Mendely入门与介绍
- printf函数具体格式
- java 复习
- [Python]在Python中添加自定义模块
- oracle 索引
- 百度文学正式成立 原创平台以纵横中文网为核心
- 日记20141128--一个截取字符串的函数
- MySQL命令使用