2.6 给定一个有环链表,实现一个算法返回环路的开头结点。

类似leetcode中 Linked List Cycle II

C++实现代码:

#include<iostream>
#include<new>
using namespace std; struct ListNode
{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL) {}
}; void createList(ListNode *&L)
{
int arr[]= {,,,,,,,,,};
int i;
ListNode *p=NULL;
ListNode *t=NULL;
for(i=; i<; i++)
{
ListNode *tmp=new ListNode(arr[i]);
if(L==NULL)
{
L=tmp;
p=tmp;
}
else
{
if(arr[i]==)
t=tmp;
p->next=tmp;
p=tmp;
}
}
p->next=t;
} ListNode *detectCycle(ListNode *L)
{
if(L==NULL)
return NULL;
ListNode *pre=L;
ListNode *p=L;
while(p&&p->next)
{
p=p->next->next;
pre=pre->next;
if(pre==p)
break;
}
if(p==NULL||p->next==NULL)
return NULL;
p=L;
while(p!=pre)
{
p=p->next;
pre=pre->next;
}
return p;
} int main()
{
ListNode *head=NULL;
createList(head);
ListNode *p=NULL;
p=detectCycle(head);
if(p)
cout<<p->val<<endl;
}

careercup-链表 2.6的更多相关文章

  1. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  2. [CareerCup] 2.2 Kth to Last Element of Linked List 链表的倒数第k个元素

    2.2 Implement an algorithm to find the kth to last element of a singly linked list. 这道题让我们求链表中倒数第k个元 ...

  3. [CareerCup] 2.3 Delete Node in a Linked List 删除链表的节点

    2.3 Implement an algorithm to delete a node in the middle of a singly linked list, given only access ...

  4. [CareerCup] 2.4 Partition List 划分链表

    2.4 Write code to partition a linked list around a value x, such that all nodes less than x come bef ...

  5. [CareerCup] 2.6 Linked List Cycle 单链表中的环

    2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...

  6. [CareerCup] 2.7 Palindrome Linked List 回文链表

    2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...

  7. [CareerCup] 4.4 Create List at Each Depth of Binary Tree 二叉树的各层创建链表

    4.4 Given a binary tree, design an algorithm which creates a linked list of all the nodes at each de ...

  8. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  9. 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST

    引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...

  10. Careercup - Google面试题 - 5735304249999360

    2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...

随机推荐

  1. 关于ARP欺骗与MITM(中间人攻击)的一些笔记( 二 )

    一直没有折腾啥东西,直到最近kali Linux发布,才回想起应该更新博客了….. 再次说明,这些技术并不是本人原创的,而是以前记录在Evernote的旧内容(排版不是很好,请谅解),本文是继关于AR ...

  2. JavaScript clearInterval定时器失效原因

    检查下是不是setInterval重复执行(两次以上<!-- 文件上传板块 --> <div id="adUploadWindow" class="ea ...

  3. VS2010 C# 调用Web Service .

    转自:http://blog.csdn.net/fenglailea/article/details/7262566 打开VS2010,打开"文件-新建-网站",选择"A ...

  4. HDU 3308 LCIS 线段树区间更新

    最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛  ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...

  5. 【Xamarin 跨平台机制原理剖析】

    原文:[Xamarin 跨平台机制原理剖析] [看了请推荐,推荐满100后,将发补丁地址] Xamarin项目从喊口号到现在,好几个年头了,在内地没有火起来,原因无非有三,1.授权费贵 2.贵 3.原 ...

  6. BZOJ_1565_[NOI2009]_植物大战僵尸_(Tarjan+最大流+最大权闭合图)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1565 n*m的矩阵,可以种植植物,僵尸从图的右边进入吃植物.前面的植物可以保护后面的植物,还有 ...

  7. android 获取设备唯一标识完美解决方案

    /** * deviceID的组成为:渠道标志+识别符来源标志+hash后的终端识别符 * * 渠道标志为: * 1,andriod(a) * * 识别符来源标志: * 1, wifi mac地址(w ...

  8. Android 应用页面延缓载入

    1.新建一个线程,使用handle的延缓运行线程 new Handler().postDelayed(new Runnable() { // 为了减少代码使用匿名Handler创建一个延时的调用 pu ...

  9. android报错——java.lang.ClassNotFoundException[android]

    E/AndroidRuntime(1875): Caused by: java.lang.ClassNotFoundException:XXXXX    in loader dalvik.system ...

  10. HDU-1874 畅通工程续 (最短路径启蒙题)

    hdu 1874比较基础,拿来练各种刚学会的算法比较好,可以避免好多陷阱,典型的最短路模板题 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memor ...