今天宿舍哥们去面X公司的内推实习生,说二面时上来就两道算法题,30分钟写完。一道是Leetcode上的原题,即判断一个链表是否有环,若有环,找出环开始的位置;还有一道题目为字符串压缩,即给出”aaabbbbccccc”,return “3a4b5c”,两道题目都比较简单;;最近在按分类刷Leetcode,即将完成String的部分,特意把Linked List Cycle I/II找出出来,提前做一下,两道题在Leetcode上面AC率都比较高。
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow = head;
ListNode *fast = head;
while (fast != NULL && fast -> next != NULL) {
fast = fast -> next -> next;
slow = slow -> next;
if (fast == slow)
return true;
}
return false;
}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
while (fast != NULL && fast -> next != NULL) {
fast = fast -> next -> next;
slow = slow -> next;
if (fast == slow)
break;
}
if (fast == NULL || fast -> next == NULL)
return NULL;
slow = head;
while (slow != fast) {
slow = slow -> next;
fast = fast -> next;
}
return slow;
}
};