63_leetcode_Linked List Cycle II
来源:互联网 发布:c程序员网站 编辑:程序博客网 时间:2024/06/15 04:33
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?
1:特殊情况;2:判断链表是否有环,如果有环的话,返回true, 并保存fast和slow碰头的节点(meetNode), 如果没有换直接返回false;3:分别从头节点往后以及meetNode节点向后遍历,当两者相同时,即为cycle开始节点。
ListNode *detectCycle(ListNode *head) { if(head == NULL || head->next == NULL) { return NULL; } ListNode* meetNode = NULL; bool flag = hasCycleCore(head, meetNode); if(flag == false) { return NULL; } else { ListNode *index = head; while(index != meetNode) { index = index->next; meetNode = meetNode->next; } return meetNode; } } bool hasCycleCore(ListNode* head, ListNode* &meetNode) { ListNode *slow = head; ListNode *fast = head; while(fast && fast->next) { slow = slow->next; fast = fast->next->next; if(slow == fast) { meetNode = slow; return true; } } return false; }
0 0
- 63_leetcode_Linked List Cycle II
- 62_leetcode_Linked List Cycle
- 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
- Leetcode_linked-list-cycle-ii
- Linked List Cycle II
- Linked List Cycle II
- 思科网络应用案例大盘点之——医疗&制造篇
- 超简单的NDK单步调试方法
- springMVC(七)视图解析器的配置和使用
- Lightweight Synchronized
- 赢微信平台:如何选择微信营销运营服务
- 63_leetcode_Linked List Cycle II
- 用反射的方式获取父类中的所有属性和方法
- 查询所有连接到ORACLE服务器的客户端IP地址的方法
- SQL Server数据类型表
- 编译原理课设——《TINY编译器》,用yacc生成语法扫描实现编译器
- 龙书,《编译原理》的别名
- springMVC教程(八)controller中方法的返回值类型
- 金融机构分类
- 访问action和访问连接的不同