LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法
题目地址:Linked List Cycle - LeetCode
Given a linked list, determine if it has a cycle in it.
To represent a cycle in the given linked list, we use an integer pos which represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Example 1:
Input: head = [3,2,0,-4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the second node.
Example 2:
Input: head = [1,2], pos = 0
Output: true
Explanation: There is a cycle in the linked list, where tail connects to the first node.
Example 3:
Input: head = [1], pos = -1
Output: false
Explanation: There is no cycle in the linked list.
图见题目网址
Follow up:
Can you solve it using O(1) (i.e. constant) memory?
这道题目是早上**现场面试的编程题。
我当时想到的解法是用set来存所有节点,如果遍历到已经存在的节点,那么这个链表存在环。
Python解法如下:
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def hasCycle(self, head: ListNode) -> bool:s=set()while head!=None:if head in s:return Trues.add(head)head=head.nextreturn False
时间复杂度和空间复杂度都是O(n)。
面试官问我有其他解法吗,我想不出来。。。。。。。。。。。。。
空间复杂度O(1)的做法是使用快慢指针
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def hasCycle(self, head: ListNode) -> bool:if head is None:return Falseslow, fast = head, head.nextwhile fast is not None and fast.next is not None:slow = slow.nextfast = fast.next.nextif slow == fast:return Truereturn False
C++解法如下:
/*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {public:bool hasCycle(ListNode *head) {if (head == nullptr || head->next == nullptr) {return false;}ListNode *slow = head;ListNode *fast = head->next;while (slow != fast) {if (fast == nullptr || fast->next == nullptr) {return false;}slow = slow->next;fast = fast->next->next;}return true;}
};
LeetCode 141. Linked List Cycle--面试编程题--C++,Python解法相关推荐
- LeetCode 141. Linked List Cycle (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- leetcode 141. Linked List Cycle
Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...
- [LeetCode] 141. Linked List Cycle 单链表判圆算法
TWO POINTER 快指针速度2 , 慢指针速度1 相对速度1,有环必然相遇 public class Solution {public boolean hasCycle(ListNode hea ...
- LeetCode 141 Linked List Cycle
用快慢指针来判定是否有环. 这里while loop里的条件,用的是fast.next != null && fast.next.next != null,保证如果没有环,slow一定 ...
- 一道经典面试逻辑题的python解法
前言: 好早之前看到的一个逻辑题:有两个2到99之间的整数,a知道这两个数的和,b知道这两个数的积. 第一句:a对b说:我不知道这两个数是多少,但我确信你也不知道. 第二句:b说:我知道了. 第三句: ...
- 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 ...
- LeetCode 148. Sort List--面试算法题--C++,Python解法
LeetCode 148. Sort List–面试算法题–C++,Python解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大 ...
- Leetcode 142. Linked List Cycle II
地址:Leetcode 142. linked list Cycle II 问题描述:检测链表是否存在环,是的话返回环入口,否则返回None. 这道题有两个思路,一个是经典的快慢指针的思路,另外一个是 ...
- 141. Linked List Cycle
欢迎fork and star:Nowcoder-Repository-github 141. Linked List Cycle 题目 Given a linked list, determine ...
最新文章
- char[1] or char*
- Apple Watch开发快速入门教程
- Delphi中uses在interfeace和implementation中的区别
- 机器学习面试题(part5)
- js 编程时注意事项
- 算法的力量(李开复)
- 【ClickHouse 技术系列】- ClickHouse 聚合函数和聚合状态
- hex转换成C语言源程序吗,第6节:把.c源代码编译成.hex机器码的操作流程
- jQuery基础部分
- 前端分页插件pagination
- 【IPC通信】基于管道的popen和pclose函数
- oracle link binaries,Oracle 单实例 Relink Binary Options 说明
- 高斯核与高斯核的卷积的结果还是一个高斯核的推导
- 怎么把office卸载干净?
- VUE 学习中的疑问
- PLM Agile BOM表结构笔记
- dubbo暴露出HTTP服务
- Springboot访问静态页面
- 跨年巨作 13万字 腾讯高工手写JDK源码笔记 带你飙向实战
- 怎样判断路由器的好坏和选择路由器?
热门文章
- RDKit | 基于Fraggle算法评估化合物的相似性
- windows共享linux的某一文件夹
- svn更新maven项目报错_使用svn管理Maven项目的方法步骤
- 【3月30日直播】新冠病毒全基因组测序——Midnight试剂盒及整体解决方案
- MPB:华大孙海汐等-从细菌基因组中预测活性前噬菌体工具Prophage Hunter的使用流程和常见问题...
- The Innovation | 中科院青促会主办精品英文期刊视频简介,定位IF 20+国际顶刊
- 微生物组-宏基因组分析第9期(报名直播课免费参加线下2020.10本年最后一期)
- mSystems:南京土壤所禇海燕组受邀发表微生物生物地理学综述(官方配视频简介)
- 面对这些情况,要勇于说“不​”
- 叶际微生物定殖模型研究进展