Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
update v2.0: //后来想到了一个不赖皮的方法 用 set 存储 判断每次存入 next 结点后 set 的 size()有没有发生变化 //没有发生变化说明这个结点已经被存储过了 就说明是个环 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { if(head == NULL) return false; set<ListNode *> s; ListNode *t; t = head; int cnt = 0; while(t->next != NULL) { s.insert(t); if(cnt == s.size()) { return true; } cnt = s.size(); t = t->next; } return false; } }; version 1.0: //感觉自己做这道题的方法有点赖皮。。。唔。。。 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: bool hasCycle(ListNode *head) { if(head == NULL) return false; ListNode *p; p = head; p->val = 99999999; while(p->next != NULL) { p = p->next; if(p->val != 99999999) { p->val = 99999999; } else { return true; } } return false; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼