LeetCode-142. Linked List Cycle II-using C/C++判断立案表内是否有环,有的话找到环的起始结点
2018-03-16 14:12
337 查看
【题目描述】 tag:Linked List difficulty:Medium
Given a linked list, return the node where the cycle begins. If there is no cycle, return
Can you solve it without using extra space?
即:尽量在不用额外空间的情况下,先判断这个链表是否有环,如果没有那么返回NULL,如果有那么返回指向开始成环的那个节点。
【函数形式】/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head)
{
}【解题思路】
判断链表是否有环的方法在141里面已经提到,不再赘述。在这基础上我们发现,如果从成环的那个节点开始用两个指针遍历链表(一个逐个遍历,另一个隔一个一遍历),那么这两个指针也将会在这个开始成环的节点相遇,基于这个发现,便有了以下这个可行方案。
【代码如下】struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* p = head;
struct ListNode* q = head;
struct ListNode* t = head;
int flag = 0; //如果这个链表有环那么flag为1,便于继续下一步寻找环的起始结点
if(head == NULL)
return NULL;
if(head->next == head)
return head;
while(p->next != NULL && q->next != NULL)
{
p = p->next->next;
q = q->next;
if(p == NULL)
return NULL;
if(p == q)
{
flag = 1;
break;
}
}
if(flag == 1)
{
if(t == p)
return head;
else
{
p = head->next;
q = head->next;
while(t != NULL)
{
t = t->next;
p = p->next->next;
q = q->next;
while(p != q)
{
p = p->next->next;
q = q->next;
}
if(t == p)
return t;
else
{
p = t->next;
q = t->next;
}
}
}
}
return NULL;
}
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?
即:尽量在不用额外空间的情况下,先判断这个链表是否有环,如果没有那么返回NULL,如果有那么返回指向开始成环的那个节点。
【函数形式】/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head)
{
}【解题思路】
判断链表是否有环的方法在141里面已经提到,不再赘述。在这基础上我们发现,如果从成环的那个节点开始用两个指针遍历链表(一个逐个遍历,另一个隔一个一遍历),那么这两个指针也将会在这个开始成环的节点相遇,基于这个发现,便有了以下这个可行方案。
【代码如下】struct ListNode *detectCycle(struct ListNode *head) {
struct ListNode* p = head;
struct ListNode* q = head;
struct ListNode* t = head;
int flag = 0; //如果这个链表有环那么flag为1,便于继续下一步寻找环的起始结点
if(head == NULL)
return NULL;
if(head->next == head)
return head;
while(p->next != NULL && q->next != NULL)
{
p = p->next->next;
q = q->next;
if(p == NULL)
return NULL;
if(p == q)
{
flag = 1;
break;
}
}
if(flag == 1)
{
if(t == p)
return head;
else
{
p = head->next;
q = head->next;
while(t != NULL)
{
t = t->next;
p = p->next->next;
q = q->next;
while(p != q)
{
p = p->next->next;
q = q->next;
}
if(t == p)
return t;
else
{
p = t->next;
q = t->next;
}
}
}
}
return NULL;
}
相关文章推荐
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
- 判断两个链表是否相交,如果相交如何找到第一个相交结点。
- 每天一道LeetCode-----判断链表是否有环,如果有,找到环的入口位置
- 如何判断一个C++对象是否在堆栈上(通过VirtualQuery这个API来获取堆栈的起始地址,然后就可以得到答案了),附许多精彩评论
- LeetCode - 231. Power of Two - 判断一个数是否2的n次幂 - 位运算应用实例 - ( C++ )
- 如何判断单链表中是否有环?如何找到环中的起始节点
- 【LeetCode008-009算法/编程练习C++】string转int(atoi),判断数字是否是回文
- 判断单链表是否有环?如何找到环的“起始”点?如何知道环的长度?
- 8.判断单链表是否有环?如何找到环的“起始”点?如何知道环的长度?
- [C++]LeetCode 9: Palindrome Number(判断整数是否是回文数)
- C++里判断目录是否存在的总结
- C++】判断一个图是否有环 无向图 有向图(转载)
- C++中函数的使用---判断一个函数是否是素数
- LeetCode65——Valid Number(使用DFA)来判断字符串是否为数字
- LeetCode Valid Parentheses(判断括号是否匹配)
- [LeetCode] 判断两个链表是否有公共节点并返回第一个公共节点
- 判断一个矩阵中是否可以找到一条包含某个String的路径
- 链表——判断链表是否有环以及环的入口结点
- C++ 判断磁盘是否为可移动磁盘
- 如何判断一个C++对象是否在堆栈上