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

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的下一个为空结束
    }
};
相关推荐
zero_one_Machel3 分钟前
leetcode73矩阵置零
算法·leetcode·矩阵
^^为欢几何^^38 分钟前
lodash中_.difference如何过滤数组
javascript·数据结构·算法
立志成为coding大牛的菜鸟.1 小时前
力扣1143-最长公共子序列(Java详细题解)
java·算法·leetcode
鱼跃鹰飞1 小时前
Leetcode面试经典150题-130.被围绕的区域
java·算法·leetcode·面试·职场和发展·深度优先
ahauedu1 小时前
案例分析-Stream List 中取出值最大的前 5 个和最小的 5 个值
数据结构·list
X同学的开始3 小时前
数据结构之二叉树遍历
数据结构
AIAdvocate6 小时前
Pandas_数据结构详解
数据结构·python·pandas
jiao000017 小时前
数据结构——队列
c语言·数据结构·算法
kaneki_lh7 小时前
数据结构 - 栈
数据结构
铁匠匠匠7 小时前
从零开始学数据结构系列之第六章《排序简介》
c语言·数据结构·经验分享·笔记·学习·开源·课程设计