关于链表的环和逆置的问题:

1.判断单向链表是否有环

快慢指针

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution
{
  public:
	bool hasCycle(ListNode *head)
	{
		ListNode *slow = head;
		ListNode *fast = head;
		while (fast && fast->next)
		{
			slow = slow->next;
			fast = fast->next->next;
			if (slow == fast)
				return true;
		}
		return false;
	}
};

2.判断单向链表是否有环及找环入口

题目描述:

Given a linked list, return the node where the cycle begins. If there is no cycle, return null.

Note: Do not modify the linked list.

Follow up:
Can you solve it without using extra space?

题目分析:就是判断并找到一条单链表相交的起始节点,如果没有相交,返回 NULL 即可 。

解题思路:先来看下面的这幅图

这里写图片描述
给一个快指针,一个慢指针 。假设quick,slow指针在Z点相遇,则slow走了a+b;quick走了a+b+c+b;

因为quick速度是slow的2倍,有

则slow走了a+b;quick走了a+b+c+b;
由时间相同得到(设速度为v) :
	(a+b)/v = (a+b+c+b)/2v 
	2a+2b   = a+b+c+b 
	       a=c

也就是a=c

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

代码:

/**
 * 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) { 
		if(head == NULL )
			return NULL ;
		ListNode *quick ,*slow;
		quick = head ;
        slow =  head ;
		bool  tag = false ;
		while(quick && quick->next ){ //注意判断条件
			slow = slow->next;
			quick = quick->next->next ; //考虑quick->next 为 NULL 的情况

			if(slow == quick ){
				tag = true ;
				break ;
			}
		}
		if(tag == false )
			return NULL ; 
		for(ListNode *fast = head ; ;){
			if(fast == slow )
				return fast ;
			fast = fast->next  ;
			slow = slow->next ;
		}
    }
};

为了证明是对的 :

这里写图片描述

2.单链表的逆置(查看下方链接)

单链表的逆置(C++实现)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值