您的位置:首页 > 其它

LeetCode之Linked List Cycle II

2013-12-26 11:42 411 查看
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;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: