???欢迎采访小残风的博客主页:残风也想永存-CSDN博客???
???本人码云 链接:残风也想永存 (FSRMWK) - Gitee.com???
有什么疑问,皆可打在评论区下,24小时不定时间进行答疑哦~,下面进入本期的主题——环形链表的求解~
一、环形链表Ⅰ
1.题目展示
2.题目链接
141. 环形链表 - 力扣(LeetCode)
3.思路讲解
4.代码实现
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */bool hasCycle(struct ListNode *head) { struct ListNode *slow = head,*fast = head; while(fast && fast ->next) { slow = slow->next; fast = fast->next->next; if(fast == slow) return true; } return false;}
5.扩展问题
a.证明:慢指针走一步,快指针走两步一定可以?
b.证明:快指针走三步、四步、....可行吗?
二、环形链表Ⅱ
1.题目展示
2.题目链接
142. 环形链表 II - 力扣(LeetCode)
3.思路讲解
4.代码实现
/** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */struct ListNode *detectCycle(struct ListNode *head) { struct ListNode *slow = head,*fast = head; while(fast && fast ->next) { slow = slow->next; fast = fast->next->next; if(fast == slow) { struct ListNode * cur = head; while(cur) { if(cur == slow) return cur; cur = cur->next; slow = slow->next; } } } return false;}