[LeetCode OJ] Linked List Cycle II 解题报告

来源:互联网 发布:java联机小游戏 编辑:程序博客网 时间:2024/04/29 16:12

题目地址:https://oj.leetcode.com/problems/linked-list-cycle-ii/

题意:判断一个链表是否自循环

解题思路:用STL的set来判重,这里set比map好。

要点:复习了set的使用

// temp1.cpp : 定义控制台应用程序的入口点。////#include "stdafx.h"#include <set>#define INF 0x7fffffffusing namespace std;/*struct ListNode {    int val;    ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};*/class Solution {    set<ListNode*> Set;public:    ListNode *detectCycle(ListNode *head) {        while(head != NULL){            if(Set.find(head) == Set.end()){                Set.insert(head);                head = head->next;            }            else{                return head;            }        }        return NULL;    }};

0 0
原创粉丝点击