判断单链表是否是循环链表以及找出循环链表入口

#include <iostream>
#include <vector>
#include <stack>
#include <queue>

using namespace std;


struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};


class Solution {
public:
	vector<int> postorderTraversal(TreeNode *root) {
		vector<int> res;
		if (!root){
			return res;
		}

		stack<TreeNode*> s;

		int sign = 1;
		TreeNode* p = NULL;

		do{
			while (root){
				//printf("val :%d in stack\n", root->val);
				s.push(root);
				root = root->left;
			}

			p = NULL;
			sign = 1;
			while (!s.empty() && sign){
				root = s.top();

				if (root->right == p){
					res.push_back(root->val);
					p = root;
					s.pop();
				}
				else{
					sign = 0;
					root = root->right;
				}
			}
		} while (!s.empty());

		return res;
	}

	vector<int> preorderTraversal(TreeNode *root) {
		vector<int> res;
		if (!root){
			return res;
		}

		stack<TreeNode*> s;
		bool flag = false;

		do{
			while (root){
				s.push(root);
				root = root->left;
			}

			flag = false;

			while (!s.empty()){
				root = s.top();
				s.pop();

				printf("%d ", root->val);

				if (root->right)
				{
					root = root->right;
					flag = true;

					break;
				}
				 
			}
		} while (!s.empty() || flag);

		printf("\n");

		return res;
	}

	vector<int> midorderTraversal(TreeNode *root) {
		vector<int> res;
		if (!root){
			return res;
		}

		queue<TreeNode*> s;

		do{
			if (!s.empty())
			{
				root = s.front();
				s.pop();
			}
			
			if(root){
				printf("%d ", root->val);

				if (root->left)
				{
					s.push(root->left);
				}
				if (root->right)
				{
					s.push(root->right);
				}
			}

		} while (!s.empty());

		printf("\n");

		return res;
	}
};

class node
{
public:
	char c;
	char d;

	double i;

	short s;

}no;

class A

{

	int i;

	char c1;

};

class B:public A

{

	char c2;

};

class C:public B

{

	char c3;

};

struct LinkList{
	int val;
	LinkList* next;
	LinkList(int val):val(val){
		next = NULL;
	}
};
typedef LinkList ListNode;

void doseq(LinkList* node){
	if (!node)
	{
		return;
	}
	
	LinkList* head = node;

	LinkList* slow = head;
	LinkList* quick = head;

	while (quick)
	{
		quick = quick->next;

		if (quick)
		{
			quick = quick->next;
			slow = slow->next;
		}
	}
	
	LinkList* cur = NULL;
	stack<LinkList*> q;
	LinkList* next = slow->next;
	slow->next = NULL;
	cur = next;
	while (cur)
	{
		q.push(cur);
		next = cur->next;
		cur->next = NULL;
		cur = next;
	}
	
	cur = head;

	LinkList* cucur = NULL;
	while (!q.empty() && cur)
	{
		next = q.top();
		q.pop();

		cucur = cur->next;

		next->next = cur->next;
		cur->next = next;

		cur = cucur;
	}
}

LinkList* insert(LinkList* node, int val){
	while(node->next)
	{
		node = node->next;
	}

	LinkList* newnode = new LinkList(val);
	node->next = newnode;

	return newnode;
}

void print(LinkList* q)
{
	while (q)
	{
		printf("%d ", q->val);
		q = q->next;
	}
	
}

bool hasCycle(ListNode *head) {
	if (!head)
	{
		return false;
	}

	ListNode* slow = head;
	ListNode* quick = head;

	while (quick)
	{
		quick = quick->next;

		if (quick)
		{
			quick = quick->next;
			slow = slow->next;

			if (quick == slow)
			{
				break;
			}
		}
	}

	slow = head;
	while(slow != quick)
	{
		slow = slow->next;
		quick = quick->next;
	}

	//printf("cycle begin node:%d\n", slow->val);

	return false;
}
int main(){
	LinkList* q1 = new LinkList(1);
	insert(q1, 2);
	insert(q1, 3);
	insert(q1, 4);
	LinkList* q2 = insert(q1, 5);
	q2->next = q1;
	
	
	printf("%res:%d\n", hasCycle(q1));
}

判断循环链表很简单,就是快慢两个指针;

但是如何找循环链表入口就需要分析下, 看下面的图:


假设quick,slow指针在Z点相遇,则slow走了a+b;quick走了a+b+c+b;

因为quick速度是slow的2倍,有(a+b)/t * 2 = (a+2b+c);

也就是a=c

这个结论很给力,此时qucik、slow在Z点,而我们现在想到循环入口Y点,头结点到Y点需要a步,而a=c,z再到y是c,所以此时从x,z点走当二者相遇就是Y点了。

就是这样。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值