题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 思路: 带环链表如图所示.设置一个快指针和一个慢指针,快指针一次走两步,慢指针一次走一步.快指针先进入环,慢指针后进入环.在进入环后,可以理解为快指针追赶慢指针,由于两个指…
14-1.剪绳子 方法一 动态规划 思路:递归式为f(n)=max(f(i), f(n-i)),i=1,2,...,n-1 虽然我现在也没有彻底明白这个递归式是怎么来的,但用的时候还是要注意一下.f(i)是指长度为i时的最大乘积. 但是,上面关于f(i)的定义,当i=1,2,3时是不成立的. 因为,长度为1时,只能返回0:长度为2时,只能返回1,:长度为3时,只能返回2.但是为了方便以后的计算,强行规定f(1)=1,f(2)=2,f(3)=3. 代码 class Solution: def cu…
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 142. Linked List Cycle II 找到环的起始节点(entry node)位置. 简介 快指针(fast pointer)和慢指针(slow pointer)都从链表的head出发. slow pointer每次移动一格,而快指针每次移动两格. 如果快慢指针能相遇,则证明链表中有…
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求的结果是[1,3]. 这种题用递归去做比较方便思考,特别是这种重复的数值.递归就是只遍历当前的节点的情况,之前的不用管,之后的以当前节点为出发点思考问题. 203. Remove Linked List Elements class Solution { public: ListNode* remo…
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId=11168 或 https://leetcode.com/problems/reverse-linked-list/ Total Accepted: 101523  Total Submissions: 258623  Difficulty: Easy Reverse a singly linked lis…
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl…
今天主要写了一下offer 1-41题,余下的稍后整理 1 """ 1 镜像二叉树: 递归 """ def mirror(root): if not root: return None mirror(root.left) mirror(root.right) root.left, root.right = root.right, root.left return root """2. 链表环入口 "&quo…
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n + y = 2 (w + y) 经过化简,我们可以得到:w  = n - y; https://www.cnblogs.com/zhuzhenwei918/p/7491892.html 3.环的长度: 从入口结点或者相遇的结点移动到下一次再碰到这个结点计数 https://blog.csdn.ne…
142. Linked List Cycle II[easy] 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? 解法一: /** * Definition for singl…
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. 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.…