首页 > 其他分享 >6.链表求环||(Leetcode 42)

6.链表求环||(Leetcode 42)

时间:2023-02-09 00:35:39浏览次数:43  
标签:node head ListNode 求环 42 fast next 链表 NULL

6.链表求环||(Leetcode 42)
方法一:
#include <stdio.h>


struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

#include <set>
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        std::set<ListNode *> node_set;
        while(head){
        	if (node_set.find(head) != node_set.end()){
	        	return head;
	        }
	        node_set.insert(head);
        	head = head->next;
        }
        return NULL;
    }
};

int main(){
	ListNode a(1);
	ListNode b(2);
	ListNode c(3);
	ListNode d(4);
	ListNode e(5);
	ListNode f(6);
	a.next = &b;
	b.next = &c;
	c.next = &d;
	d.next = &e;
	e.next = &f;
	//f.next = &c;
	Solution solve;
	ListNode *node = solve.detectCycle(&a);
	if (node){
		printf("%d\n", node->val);
	}
	else{
		printf("NULL\n");
	}
	return 0;
}

方法二:
#include <stdio.h>

struct ListNode {
	int val;
	ListNode *next;
	ListNode(int x) : val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
    	ListNode *fast = head;
    	ListNode *slow = head;
    	ListNode *meet = NULL;
    	while(fast){
    		slow = slow->next;
    		fast = fast->next;
    		if (!fast){
		    	return NULL;
		    }
		    fast = fast->next;
		    if (fast == slow){
    			meet = fast;
    			break;
    		}
	    }
	    if (meet == NULL){
    		return NULL;
    	}
    	while(head && meet){
	    	if (head == meet){
	    		return head;
	    	}
	    	head = head->next;
	    	meet = meet->next;
	    }
        return NULL;
    }
};

int main(){
	ListNode a(1);
	ListNode b(2);
	ListNode c(3);
	ListNode d(4);
	ListNode e(5);
	ListNode f(6);
	ListNode g(7);
	a.next = &b;
	b.next = &c;
	c.next = &d;
	d.next = &e;
	e.next = &f;
	f.next = &g;
	g.next = &c;
	Solution solve;
	ListNode *node = solve.detectCycle(&a);
	if (node){
		printf("%d\n", node->val);
	}
	else{
		printf("NULL\n");
	}
	return 0;
}

标签:node,head,ListNode,求环,42,fast,next,链表,NULL
From: https://www.cnblogs.com/Epiephany/p/17103840.html

相关文章

  • 7.两个排序链表的交点(Leetcode 160)
    7.两个排序链表的交点(Leetcode160)方法一:#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};#include......
  • 8.翻转链表(Leetcode206)
    8.翻转链表(Leetcode206)#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};classSolution{public:......
  • 链表基础
    二、链表:初识链表基础知识:什么是链表:链表是一种通过指针串联在一起的线性结构,每一个节点由两部分组成,一个是数据域一个是指针域(存放指向下一个节点的指针),最后一个节点......
  • 1.两个排序链表归并(Leetcode 21)
    1.两个排序链表归并(Leetcode21)#include<stdio.h>structListNode{ intval; ListNode*next; ListNode(intx):val(x),next(NULL){}};classSolution{p......
  • U214268 不老不死的竹林引路人
    不老不死的竹林引路人特别感谢icyM3tra神提供的标程OrzOrzOrz。速度上总时间直接从25s(我原来的程序)飙到了5s,单个测试点最慢也只有800ms!虽然有这么快的代码,但我还......
  • 代码随想录算法训练营Day06 | 哈希表理论基础242.有效的字母异位词 ,349. 两个数组的
    哈希表理论基础参考: 代码随想录(programmercarl.com)242.有效的字母异位词题目链接: 242.有效的字母异位词-力扣(LeetCode)题目给定两个字符串s和t,编写一个......
  • Educational Codeforces Round 142 E. Divisors and Table
    链接:https://codeforces.com/problemset/problem/1792/E题意:给定n*n乘法表,第a[i][j]=i*j,给定m=m1*m2,求m的所有因子中出现在表中的最小行的xor和。n<=1e9,m......
  • P1242 新汉诺塔
    新汉诺塔题目描述设有n个大小不等的中空圆盘,按从小到大的顺序从1到n编号。将这n个圆盘任意的迭套在三根立柱上,立柱的编号分别为A,B,C,这个状态称为初始状态......
  • P4285 [SHOI2008]汉诺塔
    [SHOI2008]汉诺塔题目描述汉诺塔由三根柱子(分别用A、B、C表示)和n个大小互不相同的空心盘子组成。一开始n个盘子都摞在柱子A上,大的在下面,小的在上面,形成了一个塔状的锥形......
  • 每日一道思维题——CF1742F - Smaller
    题意:存在字符串s,t(初始使都为"a"),有1,2两种操作方式1.将s后面+d个字符串x2.将t后面+d个字符串x操作完成后,询问是否可以改变字符串s,t中字符顺序,使得s字典序小于t若可,输......