LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II

链表相关题

141. Linked List Cycle

Given a linked list, determine if it has a cycle in it.

Follow up:
Can you solve it without using extra space? (Easy)

分析:

采用快慢指针,一个走两步,一个走一步,快得能追上慢的说明有环,走到nullptr还没有相遇说明没有环。

代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     bool hasCycle(ListNode *head) {
12         if (head == NULL) {
13             return 0;
14         }
15         ListNode* slow = head;
16         ListNode* fast = head;
17         while (fast != nullptr && fast->next != nullptr) {
18             slow = slow->next;
19             fast = fast->next->next;
20             if (slow == fast) {
21                 return true;
22             }
23         }
24         return false;
25     }
26 };

142. Linked List Cycle II

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?(Medium)

分析:

1)同linked-list-cycle-i一题,使用快慢指针方法,判定是否存在环,并记录两指针相遇位置(Z);

2)将两指针分别放在链表头(X)和相遇位置(Z),并改为相同速度推进,则两指针在环开始位置相遇(Y)。

证明如下:

如下图所示,X,Y,Z分别为链表起始位置,环开始位置和两指针相遇位置,则根据快指针速度为慢指针速度的两倍,可以得出:

2*(a + b) = a + b + n * (b + c);即

a=(n - 1) * b + n * c = (n - 1)(b + c) +c;

注意到b+c恰好为环的长度,故可以推出,如将此时两指针分别放在起始位置和相遇位置,并以相同速度前进,当一个指针走完距离a时,另一个指针恰好走出 绕环n-1圈加上c的距离。

故两指针会在环开始位置相遇。

代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode *detectCycle(ListNode *head) {
12         if(head == nullptr) {
13             return 0;
14         }
15         ListNode* slow = head;
16         ListNode* fast = head;
17         while (fast != nullptr && fast->next != nullptr) {
18             slow = slow -> next;
19             fast = fast -> next -> next;
20             if(slow == fast){
21                 break;
22             }
23         }
24         if (fast == nullptr || fast->next == nullptr) {
25             return nullptr;
26         }
27         slow = head;
28         while (slow != fast) {
29             slow = slow->next;
30             fast = fast->next;
31         }
32         return slow;
33     }
34 };
时间: 2024-08-06 19:59:19

LeetCode141 Linked List Cycle. LeetCode142 Linked List Cycle II的相关文章

leetcode142 Linked List Cycle II

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. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x),

leetcode之Linked List Cycle以及Linked List Cycle II

Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 摘自剑指offer:当用一个指针遍历链表不能解决问题的时候,可以尝试用两个指针来遍历链表,可以让一个指针遍历的速度快一些,或者让他先在链表上走若干步. 相关的题目有: 求链表的中间节点, 判断一个单向链表是否形成了环形结构 判断有环链表的环的入口节点 一次遍历求链表的倒数第K个

141. Linked List Cycle && 142. Linked List Cycle II

141. Linked List Cycle Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solution { pu

LeetCode (27) Linked List Cycle (判断cycle存在、寻找cycle入口节点)

判断cycle存在 Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? 本题要求判断给定链表中是否存在循环.并且题目要求不要使用extra space,因此,我们就不能保存每一个结点的value,在遍历的时候判断是否是循环.这道题可以通过使用速度不同的pointer进行遍历,若两个pointer相遇则说明存在cycle,若遇到N

JAVA-Linked List Cycle I&&Linked List Cycle II

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? 第一题要求是否有环 从图中可以看出 使用快慢指针 只要有环两者必定会相遇  另外从相遇点可以看出 相遇点距离环的起始点的距离与head到起始点的距离是相同的 只要再设一指针 与slow一起走 相遇点

LeetCode[Linked List]: Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example, Given 1->2->3->3->4->4->5, return 1->2->5. Given 1->1->1->2->3, return 2->3

[Linked List]Remove Duplicates from Sorted List II

Total Accepted: 59433 Total Submissions: 230628 Difficulty: Medium Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. For example,Given 1->2->3->3->4->4->5, ret

Jan 12 - Delete Node in a Linked List; Data Structure; Linked List; Pointer;

代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public void deleteNode(ListNode node) { if(node == null) return; while(node.next != null)

Jan 10 - Reverse Linked List;Data Structure; Linked List; Pointer; Iteration & Recursion

Iteration: 代码: /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */public class Solution { public ListNode reverseList(ListNode head) { if(head == null) return null; List