关闭

【Leetcode】Linked List Cycle II

455人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/linked-list-cycle-ii/

题目:

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?

思路:

首先确定循环是否存在,若存在,根据 循环结点个数/结点相对移动次数 就会相遇的规律 得到循环结点个数,再从头开始遍历,相对移动速度为结点个数,此时两指针第一次相遇的位置就是循环开始。

算法:

	public ListNode detectCycle(ListNode head) {
		ListNode q = head, p = null;
		boolean flag = false;
		// 确定循环存在
		if (q == null || q.next == null) {
			flag = false;
		} else {
			p = q.next.next;
			while (q != null) {
				if (q == p) {
					flag = true;
					break;
				}
				if (p == null || p.next == null) {
					flag = false;
					break;
				}
				p = p.next.next;
				q = q.next;
			}
			if (q == null) {
				flag = false;
			}
		}
		// ===找到循环开始节点
		if (flag == true) {
			p = q.next;//此时p/q都在循环中
			int length = 1;
			while (q != p) { // 确定循环节点长度
				p = p.next;
				length++;
			}
			q = head; 
			boolean status = true;
			while (status) {
				p = q;
				int tmp = length;
				while (--tmp >= 0) { // 指向q前length个节点p 
					p = p.next;
				}
				if (p == q) {//如果q等于开始q的前length个结点,此时p为循环开始
					status = false;
					head = p;
				}
				q = q.next;

			}
			return head;
		} else {
			return null;
		}
	}


0
0
查看评论

(Leetcode 142)Linked List Cycle (II) (快慢指针详解)

Leetcode141题和142题很相似,都涉及到了快慢指针的算法。快慢指针就是设置两个指针,一个快指针,一个慢指针来达到解题的目的。下面分为以下几个模块来讲解: 解第141题解第142题为什么快指针一定要设置为慢指针的2倍快慢指针的拓展应用 一、Leetcode 141题 题目为:Link...
  • willduan1
  • willduan1
  • 2016-03-20 21:05
  • 8265

leetcode:142. Linked List Cycle II(Java)解答

转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705 题目地址:https://leetcode.com/problems/linked-list-cycle-ii/ Linked List Cycle II Given...
  • u012975705
  • u012975705
  • 2015-12-27 16:36
  • 2189

[Leetcode-142] Linked List Cycle II(链表有环详细分析)

环的长度是多少? 如何找到环中第一个节点(即Linked List Cycle II)? 如何将有环的链表变成单链表(解除环)? 如何判断两个单链表是否有交点?如何找到第一个相交的节点?
  • xy010902100449
  • xy010902100449
  • 2015-10-09 09:33
  • 1402

leetcode141: Linked List Cycle

Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?大家考虑这样一个问题,链表的环相当于一个圆形操场。假设有两个人在圆形操场上无限循...
  • u010111016
  • u010111016
  • 2016-05-17 10:30
  • 1530

【LeetCode-面试算法经典-Java实现】【142-Linked List Cycle II(单链表中有环II)】

【142-Linked List Cycle II(单链表中有环II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a linked list, return the node where the cycle begins. If there is no ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-19 06:38
  • 3036

【LeetCode】141 Linked List Cycle (java实现)

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 题目很明确,给定一个链表,判断其中是否包含一个环路。有个额外的要...
  • styshoo
  • styshoo
  • 2015-09-02 23:34
  • 1148

LeetCode 141 — Linked List Cycle(C++ Java Python)

题目:http://oj.leetcode.com/problems/linked-list-cycle/ Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without usin...
  • dragon_dream
  • dragon_dream
  • 2014-02-27 21:26
  • 2286

LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)

判断cycle存在Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?本题要求判断给定链表中是否存在循环。并且题目要求不要使用extr...
  • angelazy
  • angelazy
  • 2015-04-28 14:36
  • 1159

【LeetCode】Reverse Linked List II 解题报告

【题目】 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5-&...
  • ljiabin
  • ljiabin
  • 2014-12-05 15:59
  • 4227

LeetCode(92)ReverseLinkedList2

题目如下: Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n =...
  • feliciafay
  • feliciafay
  • 2014-01-28 01:06
  • 2425
    个人资料
    • 访问:568042次
    • 积分:8667
    • 等级:
    • 排名:第2676名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论