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?

 /*************************************************************************
> File Name: LeetCode142.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Mon 16 May 2016 19:46:12 PM CST
************************************************************************/ /************************************************************************* 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? ************************************************************************/ #include <stdio.h> /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode *detectCycle(struct ListNode *head)
{ struct ListNode *fast = head;
struct ListNode *slow = head; int count1 = ;
int count2 = ;
while( slow && fast && fast->next )
{
fast = fast->next->next;
slow = slow->next; if( slow == fast )
{
slow = head;
while( slow != fast )
{
slow = slow->next;
fast = fast->next;
}
return slow;
}
}
return NULL;
}

LeetCode 142的更多相关文章

  1. [LeetCode] 142. Linked List Cycle II 单链表中的环之二

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

  2. LeetCode 142. 环形链表 II(Linked List Cycle II)

    142. 环形链表 II 142. Linked List Cycle II 题目描述 给定一个链表,返回链表开始入环的第一个节点.如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整 ...

  3. Java for LeetCode 142 Linked List Cycle II

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

  4. [LeetCode] 142. Linked List Cycle II 链表中的环 II

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

  5. Java实现 LeetCode 142 环形链表 II(二)

    142. 环形链表 II 给定一个链表,返回链表开始入环的第一个节点. 如果链表无环,则返回 null. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始 ...

  6. leetcode 142. Linked List Cycle II 环形链表 II

    一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点  head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...

  7. leetcode 142. Linked List Cycle II

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

  8. leetcode 142. Linked List Cycle II ----- java

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

  9. LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java

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

随机推荐

  1. 【转】Nginx系列(五)--nginx+tomcat实现负载均衡

    原博文出于:  http://blog.csdn.net/liutengteng130/article/details/47129909   感谢! Nginx占有内存少,并发能力强,事实上Nginx ...

  2. Base64编解码(C++版)

    #include <string> using namespace std; class ZBase64 { public:     /*编码     DataByte         [ ...

  3. JSP学习初体验

    JSP简介: 1)JSP--Java Server Pages 2)拥有servlet的特性与优点(本身就是一个servlet) 3)直接在HEML中内嵌JSP代码 4)JSP程序由JSP Engin ...

  4. ocp 1Z0-051 23-70题解析

    23. Examine thestructure proposed for the TRANSACTIONS table: name Null Type TRANS_ID NOT NULLNUMBER ...

  5. Beginning OpenGL ES 2.0 with GLKit Part 1

    Update 10/24/12: If you’d like a new version of this tutorial fully updated for iOS 6 and Xcode 4.5, ...

  6. cocos2dx搭建开发环境

    windows7 64位 搭建cocos2dx 版本开发环境 目前cocos2dx分为2.x版本和3.x版本,搭建环境稍有不同 先搭建3.1版本win32开发环境 相关准备: 注意:安装路径尽可能不要 ...

  7. c++的操作符格式记录

    以下摘自维基百科,mark一下,以备不时之需. For the purposes of this table, a, b, and c represent valid values (literals ...

  8. MFC视图切换大全总结

    单纯视图之间的切换 单文档多视图切换是我在学习MFC中遇到的一个老大难问题,在今天总算是一一破解了.我觉得视图切换分为三个等级,第一是在未切分窗格的情况下切换视图类:第二是在分割窗格的一个窗格内实行视 ...

  9. PostgreSQL的 initdb 源代码分析之十三

    继续分析: /* Bootstrap template1 */ bootstrap_template1(); 展开: 我这里读入的文件是:/home/pgsql/project/share/postg ...

  10. 处理get中的中文乱码情况

    1 最基本的乱码问题.这个乱码问题是最简单的乱码问题.一般新会出现.就是页面编码不一致导致的乱码.<%@ page language="java" pageEncoding= ...