Leetcode 141. Linked List CycleJAVA语言

Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?

题意:判断链表有没有环

/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public boolean hasCycle(ListNode head) {
        ///定义两个快慢指针。有环的话快指针一定会和慢指针相遇。否则快指针就提前到尾巴了
        ////应该是我那会的考研题ca
        if(head==null || head.next==null || head.next.next==null)return false;
        ListNode fast=head.next.next;
        ListNode slow=head.next;
        while(fast!=slow){
            if(fast.next!=null && fast.next.next!=null){
                fast=fast.next.next;
                slow=slow.next;    
            }else{
                //fast提前走到了尾
                return false;
            }
            
        }
        return true;
    }
}

PS:快慢指针。注意判断边界条件

时间: 2024-10-27 04:59:16

Leetcode 141. Linked List CycleJAVA语言的相关文章

leetCode 141. Linked List Cycle 链表

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? 题目大意: 判断一个单链表是否存在环. 思路: 采用快慢指针来处理. 代码如下: /**  * Definition for singly-linked list.  * struct ListNode {  *     int va

leetcode 141 Linked List Cycle Hash fast and slow pointer

Problem describe:https://leetcode.com/problems/linked-list-cycle/ 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 lis

LeetCode(141): Linked List Cycle

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? 题意:判断一个链表中是否存在环. 思路:(参考别人的做法)采用"快慢指针"检查链表是否含有环.即让一个指针一次走一步,另一个指针一次走两步,如果链表中含有环,快的指针会再次和慢的指针相遇. 代码: public boolean h

[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 extra space? 题意: 给定一个链表,判断是否有环 思路: 快慢指针 若有环,则快慢指针一定会在某个节点相遇(此处省略证明) 代码: 1 public class Solution { 2 public boolean hasCycle(ListNode head) { 3 ListNode f

LeetCode 141. Linked List Cycle(判断链表是否有环)

题意:判断链表是否有环. 分析:快慢指针. /** * 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) { ListNode* fast = head; ListNode

Leetcode 142 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? 同Leetcode 141 Linked List Cycle 性质:distance from head to 环开始点 == distance from 双指针相遇的点 to 环开始点 证明: 指

<LeetCode OJ> 141. Linked List Cycle

141. Linked List Cycle My Submissions Question Total Accepted: 88665 Total Submissions: 241622 Difficulty: Medium Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using extra space? Subscribe to see which co

LeetCode OJ Linked List: 138题、109题和191题

138题:Copy List with Random Pointer 题目分析: 本题思路1:第一步,你需要遍历一下链表,对于每个结点,你都new出一个连接在其后面.第二步,调整random指针.第三步,把复制的链表与原链表断开.时间复杂度O(N),空间复杂度O(1). 本题思路2:第一步,仍需要遍历一下链表,对于每个结点都new出一个节点,但不连接在其后面,把这种旧节点到新结点的映射关系,存储在map中.第二步,调整random指针.时间复杂度O(N),空间复杂度O(N). 本题思路3:第一步

LeetCode 1 : Linked List

分组 328. Odd Even Linked List 序号为奇数的在前,偶数的在后,每一半相对顺序不变. class Solution { public: ListNode* oddEvenList(ListNode* head) { if(!head) return NULL; int index = 1; ListNode pseudoHeadOfNewList = ListNode(0); ListNode pseudoHeadOfOldList = ListNode(0); pseu