leetcode--Linked List Cycle II
来源:互联网 发布:费县消失的夫妻知乎 编辑:程序博客网 时间:2024/06/11 16:08
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?
算法:
点击打开链接
java:
/** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */public class Solution { public ListNode detectCycle(ListNode head) { if(null==head||null==head.next){ return null; } ListNode p,q,s,join=null; p=head; q=head; s=head; while(q!=null){ p=p.next; q=q.next; if(q!=null) q=q.next; else{ return null; } if(p==q){ join=q; break; } } if(join!=null){ while(join!=s){ join=join.next; s=s.next; } return s; } return null; }}
c++:
/** * 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||!head->next) return NULL; ListNode *p,*q,*s,*start; p=head; q=head; s=NULL; while(q){ q=q->next; p=p->next; if(q){ q=q->next; }else{ return NULL; } if(p==q){ s=p; break; } } if(s){ start=head; while(s!=start){ s=s->next; start=start->next; } return start; } return NULL; }};
0 0
- 【LeetCode】 Linked List Cycle II
- Leetcode: Linked List Cycle II
- <Leetcode>Linked List Cycle II
- [LeetCode] Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode:Linked List Cycle II
- Leetcode Linked List Cycle II
- [LeetCode]Linked List Cycle II
- leetcode Linked List Cycle II
- LeetCode | Linked List Cycle II
- Leetcode: Linked List Cycle II
- [LeetCode] - Linked List Cycle II
- Leetcode: Linked List Cycle II
- LeetCode - Linked List Cycle II
- leetcode Linked List Cycle II
- Linked List Cycle II -- LeetCode
- LeetCode Linked List Cycle II
- [LeetCode] Linked List Cycle II
- windows 下安装ssh 登陆linux
- 游标
- A Theoretical Analysis of Feature Pooling in Visual Recognition(泛读)
- glibc定义的系统调用
- activiti modeler流程设计器界面定制
- leetcode--Linked List Cycle II
- N-Queens
- Ubuntu 下安装yaf 框架
- MySQL中的数据类型
- 支持向量机
- MVC JQuery Validate使用总结
- unity4.6 UGUI做技能CD(补充篇)
- 纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦等)
- solr与hadoop结合