struct ListNode* EntryNodeOfLoop(struct ListNode* pHead ) {
// write code here
if (pHead == NULL || pHead->next == NULL) {
return NULL;
}
struct ListNode* slow = pHead->next;
struct ListNode* fast = pHead->next->next;
while (fast != NULL && fast->next != NULL) {
if (slow == fast) {
// 链表中存在环
slow = pHead;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
slow = slow->next;
fast = fast->next->next;
}
return NULL; // 链表中不存在环
}
// write code here
if (pHead == NULL || pHead->next == NULL) {
return NULL;
}
struct ListNode* slow = pHead->next;
struct ListNode* fast = pHead->next->next;
while (fast != NULL && fast->next != NULL) {
if (slow == fast) {
// 链表中存在环
slow = pHead;
while (slow != fast) {
slow = slow->next;
fast = fast->next;
}
return slow;
}
slow = slow->next;
fast = fast->next->next;
}
return NULL; // 链表中不存在环
}
「求助大佬帮看看这道算法题吧!」给一个长度为n链表,若其中包含环,请找出该链表的环的入口结点,否则,返回null。 数据范围: , 要求:空间复杂度 ,时间复杂度 例如,输入{1,2},{3,...
https://gw-c.nowcoder.com/api/sparta/jump/link?link=https%3A%2F%2Fwww.nowcoder.com%2Fpractice%2F253d2c59ec3e4bc68da16833f79a38e4
全部评论
相关推荐
点赞 评论 收藏
分享
点赞 评论 收藏
分享