http://oj.leetcode.com/problems/linked-list-cycle-ii/
还是给定一个链表,查看是不是有环,有环的话返回 环的起始节点。
为了 without using extra space, 遍历某节点后更改val用以表明该节点已遍历过
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
while(head) {
if(head->next == NULL) break;
if(head->val == 11111) {
return head;
}
head->val = 11111;
head = head->next;
}
return NULL;
}
};