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. js template

    http://garann.github.io/template-chooser/ http://www.gbin1.com/technology/javascript/20120917-javasc ...

  2. Android Studio的一些技巧和使用注意事项(持续更新)

    1.创建一个项目之后默认是没有assets目录的,可以手动在main目录下创建一个assets目录. 2.

  3. "NetworkError: 500 Internal Server Error - http://develop.console.aliyun.sinopec.com/ots/ots_queryOtsList.action?state=0"

    项目明明开始好好的,报的这个错,错误提示也很少,啥信息都没有,只是明白是服务器报了500,知道是服务器内部错误,但是却没法找不到问题所在.后来突然想到把下面报错的action直接在浏览器运行: htt ...

  4. const char*, char const*, char*const的区别

    http://www.cnblogs.com/aduck/articles/2244884.html

  5. 【HDOJ】5096 ACM Rank

    Treap+set仿函数重定义.每当ac一道题目时,相当于对总时间减去一个大数. /* 5096 */ #include <iostream> #include <string> ...

  6. Android Http Server

    Android Http Server 1 引言          Android如何构建Http服务器呢?本文的小例子,约莫着,还是能做个参考的^^.恩,例子实现的是PC浏览手机文件,支持了下载和删 ...

  7. Git报错:insufficient permission for adding an object to repository database .git/objects

    在本地搭建Git服务器后,在开发机上push新代码,发现Git提示: insufficient permission for adding an object to repository databa ...

  8. python 中 time 模块 格式化 format

    %y 两位数的年份表示(00-99)%Y 四位数的年份表示(000-9999)%m 月份(01-12)%d 月内中的一天(0-31)%H 24小时制小时数(0-23)%I 12小时制小时数(01-12 ...

  9. python导入上级目录中的模块

    python导入同级别模块很方便: import xxx 要导入下级目录页挺方便,需要在下级目录中写一个__init__.py文件 from dirname import xxx 要导入上级目录,可以 ...

  10. linux时间设置相关

    1.查询时间命令:date 2.设置日期:date -s mm/dd/yyyy 3.设置时间:date -s HH:MM:SS 4.将当前时间及日期写入BIOS,避免重启失效:hwclock -w 5 ...