LeetCode:142. 环形链表 II

    技术2022-08-10  95

    /** * 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) { set<ListNode *>obj;///为了防止元素内容相同,这里选择存储整个链表的节点 while(head) { if(obj.count(head)==1) { return head; } obj.insert(head); head=head->next; } return NULL; } };
    Processed: 0.019, SQL: 9