链表快慢指针合集(力扣)

876. 链表的中间结点

代码

复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
         ListNode * slow = head, * fast = head;
        while(fast && fast -> next){
            slow = slow->next;
            fast = fast->next->next;
        }//找中间节点
        return slow;
    }
};

876. 链表的中间结点

代码

复制代码

141. 环形链表

代码

复制代码
/**
 * 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 * slow = head, * fast = head;
        while(fast && fast -> next){
            slow = slow->next;
            fast = fast -> next -> next;
            if(slow == fast) return true;
        }
        return false;

    }
};

142. 环形链表 II

代码

复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        ListNode * slow = head, *fast = head;
        while(fast && fast -> next){
            slow = slow -> next;
            fast = fast -> next -> next;
            if(slow == fast){
                while(head != slow){
                    head = head->next;
                    slow = slow -> next;
                }
                return slow;
            }
        }
        return NULL;
    }
};

LCR 026. 重排链表

代码

复制代码
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    void reorderList(ListNode* head) {
        ListNode * slow = head, * fast = head;
        while(fast && fast -> next){
            slow = slow->next;
            fast = fast->next->next;
        }//找中间节点
        ListNode * prev = nullptr, * cur = slow;
        while(cur){
            ListNode * tp = cur -> next;
            cur -> next = prev;
            prev = cur;
            cur = tp;
        }//反转后半段链表
        ListNode * head2 = prev, *head1 = head;
        while(head2 -> next){
            ListNode * tp1 = head1->next, * tp2 = head2 -> next;
            head2->next = head1->next;
            head1->next = head2;
            head1 = tp1;
            head2 = tp2;
        }//令交错指向,最终head2的下一个为空结束
    }
};
相关推荐
小白程序员成长日记8 分钟前
2025.12.01 力扣每日一题
算法·leetcode·职场和发展
TL滕40 分钟前
从0开始学算法——第四天(练点题吧)
数据结构·笔记·学习·算法
[J] 一坚42 分钟前
华为OD、微软、Google、神州数码、腾讯、中兴、网易有道C/C++字符串、数组、链表、树等笔试真题精粹
c语言·数据结构·c++·算法·链表
多则惑少则明1 小时前
【算法题4】找出字符串中的最长回文子串(Java版)
java·开发语言·数据结构·算法
迷途之人不知返1 小时前
二叉树题目
数据结构·算法
.柒宇.2 小时前
力扣hot100---42.接雨水(java版)
java·算法·leetcode
迈巴赫车主2 小时前
蓝桥杯20534爆破 java
java·数据结构·算法·职场和发展·蓝桥杯
坚持就完事了2 小时前
数据结构之链表
数据结构·python·算法·链表
小李小李快乐不已3 小时前
图论理论基础(3)
数据结构·c++·算法·图论
星竹晨L3 小时前
C++红黑树:理论与实践相结合的平衡艺术
开发语言·数据结构·c++