[C语言][LeetCode][142]Linked List Cycle II
来源:互联网 发布:深圳市逻辑思维软件 编辑:程序博客网 时间:2024/06/03 21:07
题目
Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
标签
Linked List、Two Pointers
难度
中等
分析
题目意思是如果链表里面有回环,找出回环开始的位置,如果没有,返回null。我的做法是先判断链表是否有回环,判断回环的方法可以参照下文
http://blog.csdn.net/Timsley/article/details/51155365
如果找到回环,快指针指回表头,然后快慢指针继续走,快慢指针每次都是走一步,如果相遇,就找到这个指针,返回即可。
C代码实现
struct ListNode *detectCycle(struct ListNode *head) { struct ListNode *fast, *slow; bool fgHasCycle = false; if(!head) return NULL; fast = head; slow = head; while(fast && fast->next) { fast = fast->next->next; slow = slow->next; if(fast == slow) { fgHasCycle = true; break; } } if(true == fgHasCycle) { fast = head; while(fast) { if(fast == slow) return fast; fast = fast->next; slow = slow->next; } } else return NULL;}
0 0
- [C语言][LeetCode][142]Linked List Cycle II
- [leetcode-142]Linked List Cycle II(c)
- 【LeetCode】 142. Linked List Cycle II C语言
- LeetCode.142(141) Linked List Cycle && Linked List Cycle (II)
- [C++]LeetCode: 74 Linked List Cycle II
- LeetCode 142 Linked List Cycle II
- LeetCode 142 Linked List Cycle II
- LeetCode: Linked List Cycle II [142]
- [leetcode 142]Linked List Cycle II
- Leetcode NO.142 Linked List Cycle II
- [leetcode]142 Linked List Cycle II
- [LeetCode 142]Linked List Cycle II
- leetcode || 142、Linked List Cycle II
- 【leetcode】【单链表】【142】Linked List Cycle II
- [Leetcode 142] Linked List Cycle II
- leetcode 142: Linked List Cycle II
- leetCode #142 Linked List Cycle II
- Leetcode #142 Linked List Cycle II
- Redis内存使用优化与存储
- 希尔排序
- 哈夫曼树(POJ3253)
- 1.2.4 Linux的开发:虚拟团队的产生
- leetcode - Contains Duplicate
- [C语言][LeetCode][142]Linked List Cycle II
- Win7免安装Mysql 5.6.24版本
- 位操作运算符
- web结构
- 零零碎碎(摘抄)
- UISearchBar 去掉灰色边框
- 【C++ STL】算法 <algorithm>中各种算法解析
- MySQL学习笔记—自定义函数
- java的数据类型