careercup-链表 2.6
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的更多相关文章
- [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 ...
- [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个元 ...
- [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 ...
- [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 ...
- [CareerCup] 2.6 Linked List Cycle 单链表中的环
2.6 Given a circular linked list, implement an algorithm which returns the node at the beginning of ...
- [CareerCup] 2.7 Palindrome Linked List 回文链表
2.7 Implement a function to check if a linked list is a palindrome. LeetCode上的原题,参见我之前的博客Palindrome ...
- [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 ...
- [CareerCup] 17.13 BiNode 双向节点
17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...
- 二叉树系列 - 二叉搜索树 - 线性时间内把有序链表转化为BST
引言 本文来自于Google的一道题目: how to merge two binary search tree into balanced binary search tree. how to me ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
随机推荐
- asp.net中Web使用Socket
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- git操作回顾:
1. git查看自己的本地分支: ***:~/mysite/mysite$ git branch * master 2. 查看远程分支: ***:~/mysite/mysite$ git branch ...
- UVA 11426 GCD Extrme (Ⅲ)
给定一个整数N(1<N<=4000000)的整数求∑GCD(i,j)i=1,2,3....j-1,2<=j<=n的值.参考了一下网上的题解,复述一下我理解后的思路,加深理解: ...
- 打造属于自己的Altium Designer 3D封装库,不需要懂专门的三维设计软件
看到Andy_2020发的帖子“Altium Designer专题”之后,对Altium Designer的3D功能很感兴趣,着手自己做一个AD的3D封装库.刚开始按照Andy介绍的方法,学了两天So ...
- Realm for Android快速入门教程
介绍 如果你关注安卓开发的最新趋势,你可能已经听说过Realm.Realm是一个可以替代SQLite以及ORMlibraries的轻量级数据库. 相比SQLite,Realm更快并且具有很多现代数据库 ...
- C# 读取网络txt文件 并写在本地txt文件中
public void WriteStream() { try { stirng ImagesPath = ImagesPath + "\\2013-09-27.txt"; Htt ...
- 在linux系统下怎么安装两个nginx
在linux下安装nginx的时候,一般在./configure的阶段会要求通过prefix设置安装路径.因此,在./configure的时候指定不同的prefix就可以安装多个nginx啦. 值得注 ...
- 【Bower】
Bower Bower:客户端库管理工具 Day 1: Bower —— 管理你的客户端依赖关系 bower解决js的依赖管理
- DevExpress中XtraGrid控件对GridView每行的颜色设置 zt
改变行颜色 private void GridView1_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArg ...
- 同时连接多台Android设备进行Monkey Test
事情多了就会乱,时间久了就会忘. adb -s remount id(devices ID) adb -s shell id monkey -p package name -v count